尝试生成一个简单的 ActivityUnitTestCase 来测试我的 Android 代码中的各种片段。
public class MenuFragmentTest extends ActivityUnitTestCase<FragmentAdapter> {
static {
FragmentAdapter.setFragmentUnderTest(new MenuFragment());
}
public MenuFragmentTest() {
super(FragmentAdapter.class);
}
public void testMenuOptions() {
System.out.println(getActivity().findViewById(1));
}
}
我试图用来在应用程序中测试片段的通用 FragmentAdapter:
public class FragmentAdapter extends FragmentActivity {
static Fragment fragmentUnderTest;
public static void setFragmentUnderTest(Fragment fragmentUnderTest) {
FragmentAdapter.fragmentUnderTest = fragmentUnderTest;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
setContentView(ll);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(fragmentUnderTest, null);
fragmentTransaction.commit();
}
}
成功安装主应用程序后,Android JUnit Test Runner 失败并显示
java.lang.RuntimeException: Could not find test class. Class: com.XXX.core.MenuFragment
为什么 Test Runner 找不到我正在运行的测试类?