6

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

Labeeb Panampullan
  • 34,521
  • 28
  • 94
  • 112

6 Answers6

7

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.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • Thanks Dtmilano, That link you provided is really helpful, but I have added that build path for External libraries. Still bad luck for me. When I created my testing in the same project it worked :) – Labeeb Panampullan Jun 28 '11 at 05:28
  • What you published is a workaround, that will lead you to another problem: how not to ship the tests with your app. – Diego Torres Milano Jun 28 '11 at 14:50
  • Are you using Eclipse ? I'm sure it works because I'm using it. – Diego Torres Milano Jun 28 '11 at 14:51
  • Yes me using eclipse :( And if I use same project as test, will that make problem for me later?:( I think after completing my project I will delete that extra testing package and manifest entries for testing. will that be fine? – Labeeb Panampullan Jun 29 '11 at 03:35
  • follow http://dtmilano.blogspot.com/2009/12/android-testing-external-libraries.html I still met compile when run ant ant build ant debug in the test project. Such as: package android.support.v4.app does not exist [javac] import android.support.v4.app.FragmentActivity; – George_BJ Sep 18 '12 at 01:47
  • Then I add a link from the android.support.v4 in main project to my test project, the build is passed but when running test with "ant debug test ", still met Caused by: java.lang.NoClassDefFoundError: com.mobile.client.activity.QuestionListActivity – George_BJ Sep 18 '12 at 02:01
  • But there is a problem with support fragments. `fragment.getView()` returns null. But with android.app.Fragments it works fine. I'm using (extending) ActivityUnitTestCase. Any idea? – Muhammad Babar Jun 09 '15 at 06:13
7

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 :)

Matthieu
  • 16,103
  • 10
  • 59
  • 86
Labeeb Panampullan
  • 34,521
  • 28
  • 94
  • 112
  • 2
    This successfully works around it but Id still like to know why it happens when using a separate test project... – brk3 Apr 23 '12 at 13:42
1

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

Then, make sure the main project is on the build path of the test project. Do the same for any other libraries the main, and test projects might share.

ne0fhyk
  • 186
  • 1
  • 3
1

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.

Community
  • 1
  • 1
Rupert Bates
  • 3,041
  • 27
  • 20
0

I have tried almost a day to deal with this issue and found a solution from another post

Basically your android-support-v4.jar (in my case) is used in the Application should be same with what you have used in your Test application. The best way to do is, remove the same jar from the libs directory of your test project and export the same from your Application project. That way you can get rid of this issue for sure.

Community
  • 1
  • 1
Avijit
  • 21
  • 2
0

My guess is that ActivityInstrumentationTestCase2 is not compatible with Fragments. You should try something like Robolectric.

Macarse
  • 91,829
  • 44
  • 175
  • 230