我想用 AndroidTest 用例和 Mockito 测试一个片段(我正在使用 mockito 来测试其他测试用例)。
我以前用我自己的代码(以不同的方式编码)做过这个,但在这种情况下,我正在测试一个片段,我想模拟这个调用: final PackageManager packageManager = getActivity().getPackageManager();
我将把你放在这里的TestClass 的一部分,以及我想测试的Fragment 的一部分。提前感谢您的想法或建议。
public class MyFragmentTest extends
ActivityInstrumentationTestCase2<MyActivity>{
MyFragment myFragment;
public MyFragmentTest () {
super(MyActivity.class);
}
@Override
public void setUp() throws Exception {
super.setUp();
// This have to be done because of some issues with dexmaker
System.setProperty("dexmaker.dexcache", "/sdcard");
// This have to be done because of the sharedUserId problem
Thread.currentThread().setContextClassLoader(
getClass().getClassLoader());
myFragment = new MyFragment() {
//I can override methods here
};
}
public void testMyMethod() throws Exception {
myFragment.methodThatIWantToTest();
}
}
/************ CLASS THAT I WANT TO TEST *********/
public class MyFragment extends Fragment{
public void methodThatIWantToTest(){
/*..... more lines */
final PackageManager packageManager = getActivity().getPackageManager();
/*..... more lines ...*/
}
}