5

I am trying to run Instrumental tests for my project. But they don't run on devices(emulators as well) that have version lower than 5 (API 21).

I have been trying to solve this problem, but still facing with it. I get following exception.

02-15 10:46:08.965 1127-1143/? E/AndroidRuntime: FATAL EXCEPTION: Instr: android.support.test.runner.AndroidJUnitRunner
                                                 java.lang.ExceptionInInitializerError
                                                     at android.support.test.internal.runner.TestRequestBuilder.<init>(TestRequestBuilder.java:81)
                                                     at android.support.test.internal.runner.TestRequestBuilder.<init>(TestRequestBuilder.java:524)
                                                     at android.support.test.runner.AndroidJUnitRunner.createTestRequestBuilder(AndroidJUnitRunner.java:379)
                                                     at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:352)
                                                     at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:269)
                                                     at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
                                                  Caused by: java.lang.NoClassDefFoundError: org.junit.runner.manipulation.Filter$1
                                                     at org.junit.runner.manipulation.Filter.<clinit>(Filter.java:21)
                                                     at android.support.test.internal.runner.TestRequestBuilder.<init>(TestRequestBuilder.java:81) 
                                                     at android.support.test.internal.runner.TestRequestBuilder.<init>(TestRequestBuilder.java:524) 
                                                     at android.support.test.runner.AndroidJUnitRunner.createTestRequestBuilder(AndroidJUnitRunner.java:379) 
                                                     at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:352) 
                                                     at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:269) 
                                                     at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584) 

I have already tried all solutions for similar questions.

Here how my project structure looks.

project structure

And gradle dependencies for tests

  /************* TEST STUFF ***************/
    androidTestCompile 'junit:junit:4.12'
    androidTestCompile fileTree(include: ['robotium-solo-5.5.2.jar'], dir: 'libs')
    //mockito dependencies
    androidTestCompile 'org.mockito:mockito-core:2.7.6'
    androidTestCompile files('libs/dexmaker-mockito-1.0.jar')
    androidTestCompile files('libs/dexmaker-1.0.jar')

    // Set this dependency to build and run Espresso tests
    androidTestCompile('com.android.support.test.espresso:espresso-core:+') {
        exclude group: 'com.android.support', module: 'support-annotations'
    }

    androidTestCompile('com.android.support.test:runner:0.5') {
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    // Set this dependency to use JUnit 4 rules
    androidTestCompile('com.android.support.test:rules:+') {
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    androidTestCompile('com.android.support.test.espresso:espresso-intents:+') {
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    //MockWebServer - Version 2.2 of mockwebserver doesn't work because of an issue, so forcing v2.1 - https://github.com/square/okhttp/issues/1069
    androidTestCompile('com.squareup.okhttp3:mockwebserver:+') {
        exclude module: 'okhttp'
    }
    androidTestCompile "org.slf4j:slf4j-api:1.7.12"
    //WireMock
    androidTestCompile("com.github.tomakehurst:wiremock:2.5.0") {
        //Using Android Version Instead
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'

        //Version conflict with our app's slf4j version
        exclude group: 'org.slf4j', module: 'slf4j-api'

        //Was getting a classpath conflict for org.objectweb.asm.AnnotationVisitor which is a part of 'net.minidev:asm'
        exclude group: 'org.ow2.asm', module: 'asm'

        //Was getting this warning, so decided to ignore this version included by WireMock.
        //Warning:Dependency org.json:json:20090211 is ignored as it may be conflicting with the internal version provided by Android.
        //In case of problem, please repackage with jarjar to change the class packages
        exclude group: 'org.json', module: 'json'
    }
    androidTestCompile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'

And BaseTestClass have the following structure

@RunWith(AndroidJUnit4.class)
public abstract class InstrumentalSuperTest {

    private SystemAnimations mSystemAnimations;

    @Rule
    public IntentsTestRule rule = provideActivity();

    @Rule
    public WireMockRule wireMockRule = new WireMockRule(WireMockConfiguration.wireMockConfig().port(BuildConfig.PORT), false);

    protected abstract IntentsTestRule provideActivity();

}

And this happens not only with my project, but with Wiremock examples as well, the same error.

Maybe I run test incorrectly, I just click on a test class and choose Run ... Test.

Please help to solve this problem, I have no idea what is wrong.

u32i64
  • 2,384
  • 3
  • 22
  • 36
  • Is `provideActivity()` a custom method? Can you provide the content of it? – sebokopter Feb 18 '17 at 23:46
  • could be a duplicate of: https://stackoverflow.com/questions/40867407/android-espresso-multidex-fail try defining a multiDexKeepProguard file('../proguardRules/multidex-proguard.pro') in your flavor config and in the file use proguard like syntax to keep the classes you cannot find in your tests, multidex is a devil to get properly sometimes in test runs – originx Mar 03 '17 at 20:56
  • Test Instrumentation APKs before Lollipop aren't able to multi-dex, even if you try. WireMock has a huge method count footprint, and my guess is that your test APK is multidex-ed. The first dex file will be picked up, but anything in your subsequent dex files will be unavailable and you'll get a ClassNotFoundException if you try to access any of the classes in other dex files. – Sam Edwards Mar 13 '17 at 20:40
  • Have you found a solution? – Day Jul 12 '17 at 14:07

1 Answers1

1

You may try newer version of wiremock. later than 2.0.8-beta. for more about this have a look at this stackoverflow answer

best wishes
  • 5,789
  • 1
  • 34
  • 59