我有一个处理片段创建的控制器类。让我们像下面这样说:
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 对象的每个类中使用静态方法并使用它,或者是否有更好的方法?
任何这方面的例子都值得赞赏。