Me used Fragment
of Android Compatibility Package, using the android-support-v4.jar
. But I can't do the JUnit test on this.
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 as Android JUnit Test
produce FailedToCreateTests[Runner:Junit3]
Failure Trace
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
)
Me used the same android-support-v4.jar
in my both test and main project
ActivityInstrumentationTestCase2
is compatible with Fragments
. You should only follow the steps mentioned in Android Testing: External libraries, which has been updated to cover the case of the android-support-v4.jar
too.
Then, you will be able to write tests like this one:
public void testFragmentManager() {
FragmentActivity activity = getActivity();
assertNotNull(activity.getSupportFragmentManager());
}
or whatever you need to test.
I found a solution
The problem was
Caused by: java.lang.NoClassDefFoundError: com.android.myproject.MyActivityClass
It cannot find the class path even though I refer the same jar in both projects ( I also tried by using separate jar for both the project )
Now, I created my testing environment in the same 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 in my special package for testing
extends ActivityInstrumentationTestCase2<fragmentActivity>
Now everything is working fine :)
See this question for a better answer:
FragmentActivity can not be tested via ActivityInstrumentationTestCase2
You need to export the reference to the compatibility library from your app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With