Trying to generate a simple ActivityUnitTestCase to test various Fragments in my Android code.
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));
}
}
The generalized FragmentAdapter that I’m trying to use to test Fragments in the app:
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();
}
}
After the main app installs successfully, the Android JUnit Test Runner fails with
java.lang.RuntimeException: Could not find test class. Class: com.XXX.core.MenuFragment
Why can the Test Runner not find the test class I am running?
Apparently the name of the test package really does matter:
From http://developer.android.com/tools/testing/testing_android.html:
Altering the package instrumentation targetPackage attribute is not good enough to allow you to create tests in the same package as the activity under test, even if the test is in the separate test project.