Fragmentation check

Me used the Fragment of the Android Compatibility Package using android-support-v4.jar . But I can not run the JUnit test.

My main FragmentActivity class is declared as follows

 public class MyActivityClass extends FragmentActivity{ ............... } 

Then in my test project

 public class MyActivityClassTest extends ActivityInstrumentationTestCase2<MyActivityClass> { public MyActivityClassTest() { super("com.android.myproject", MyActivityClass.class); } @Override protected void setUp() throws Exception { super.setUp(); ................... } public void testPreconditions() { ................. } public void testNotNull(){ ................ } } 

But when I run how Android JUnit Test produce FailedToCreateTests[Runner:Junit3]
Crash tracking

 java.lang.RuntimeException: Exception during suite construction at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:239) at java.lang.reflect.Method.invokeNative(Native Method) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447) Caused by: java.lang.reflect.InvocationTargetException at com.android.myproject.test.MyActivityClassTest.<init>(MyActivityClassTest.java:28) at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:446) at android.test.suitebuilder.TestMethod.instantiateTest(TestMethod.java:87) at android.test.suitebuilder.TestMethod.createTest(TestMethod.java:73) at android.test.suitebuilder.TestSuiteBuilder.addTest(TestSuiteBuilder.java:263) at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:185) at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:336) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3982) at android.app.ActivityThread.access$2900(ActivityThread.java:119) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1901) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4363) at java.lang.reflect.Method.invokeNative(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NoClassDefFoundError: com.android.myproject.MyActivityClass ... 19 more 

When I changed MyActivityClass to extends Activity , it worked fine (MyActivityClass extends Activity )
I used the same android-support-v4.jar in my test and main project

+6
source share
6 answers

I found a solution

The problem was

 Caused by: java.lang.NoClassDefFoundError: com.android.myproject.MyActivityClass 

He cannot find the path to the class, although I refer to the same jar in both projects (I also tried to use a separate jar for both projects)

Now I created my test environment in one project , then it worked

In my AndroidManifest.xml

 <manifest...> <!-- For doing JUnit test, Instrumentation Start (remove later) --> <instrumentation android:targetPackage="com.pe.android.isccasinos" android:name="android.test.InstrumentationTestRunner" /> <!-- For doing JUnit test, Instrumentation End (remove later) --> <application ...> ................ <!-- For doing JUnit test, add library starting (remove later) --> <uses-library android:name="android.test.runner" /> <!-- For doing JUnit test, add library ending (remove later) --> </application> <manifest> 

Then I added my testing class to my special testing package.

 extends ActivityInstrumentationTestCase2<fragmentActivity> 

Now everything works fine :)

+7
source

ActivityInstrumentationTestCase2 compatible with Fragments . You should only follow the steps mentioned in Android Testing: External libraries that have been updated to cover the case of android-support-v4.jar too.

Then you can write tests like this:

 public void testFragmentManager() { FragmentActivity activity = getActivity(); assertNotNull(activity.getSupportFragmentManager()); } 

or what you need to check.

+7
source

See this question for a better answer:

FragmentActivity cannot be tested with ActivityInstrumentationTestCase2

You need to export the link to the compatibility library from your application.

+1
source

Late, but I solve the same problem only by adding the android-support-v4.jar dependency to the main project and removing it from the test project.

Then make sure that the main project is in the path of building the test project. Do the same for any other libraries, basically, and test projects can share.

+1
source

I assume that ActivityInstrumentationTestCase2 not compatible with Fragments . You should try something like Robolectric .

0
source

I tried almost a day to solve this problem and found a solution from another post

Basically your android-support-v4.jar (in my case) is used in the application, it should be the same as in your test application. The best way to do this is to remove the same jar from the libs directory of your test project and export it from your application project. Thus, you can surely get rid of this problem.

0
source

Source: https://habr.com/ru/post/891436/


All Articles