我有一个控制器类来处理Fragment的创建。就像下面这样:
public class FragmentController {
public static Fragment newInstance(String title, int total) {
return total > 0? MultipleDataFragment.newInstance(title, total)
: SingleDataFragment.newInstance(title);
}
}
public class MultipleDataFragment extends Fragment {
public static MultipleDataFragment newInstance( String title, int total) {
Bundle b = new Bundle();
b.putString("title", title);
b.putInt("total", total);
}
}
public class SingleDataFragment extends Fragment {
public static SingleDataFragment newInstance( String title, int total) {
Bundle b = new Bundle();
b.putString("title", title);
b.putInt("total", total);
}
}
在我的测试中(标准的Junit4测试类),我有以下代码:
@Test
public void testNewInstanceCreteMultipleData() throws Exception {
Fragment f = FragmentController.newInstance("Hello", 5);
assertTrue("MultipleDataFragment should be created"
, f instanceOf MultipleDataFragment);
}
由于我没有模拟Bundle,所以我遇到了问题。
java.lang.RuntimeException: Method putString not mocked.Set
问题是我如何模拟Bundle对象,以便可以执行测试?我需要在每个类中使用静态方法来创建Bundle对象并使用它,还是有更好的方法?
欢迎提供任何相关示例。