android – Instrumentation run failed due to 'java.io.FileNotFoundException'-ThrowExceptions

Exception or error:

I’m trying to get my instrumentation tests running on Travis CI. I’m getting this error when the build runs on Travis. However, I don’t have any problems running the tests locally. I’m new to Android/Java development so I’m not even sure where to start looking.

...

:MyappAndroid:packageMyappDebugAndroidTest
:MyappAndroid:assembleMyappDebugAndroidTest
:MyappAndroid:connectedMyappDebugAndroidTest
Tests on test(AVD) - 6.0 failed: Instrumentation run failed due to 'java.io.FileNotFoundException'

com.android.builder.testing.ConnectedDevice > No tests found.[test(AVD) - 6.0] FAILED 
No tests found. This usually means that your test classes are not in the form that your test runner expects (e.g. don't inherit from TestCase or lack @Test annotations).
:MyappAndroid:connectedMyappDebugAndroidTest FAILED

...

Here is one of my tests that I expect to run:

package core.ui.dialogs;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class DialogActivityTest {
    @Rule
    public ActivityTestRule<DialogActivity> mActivityRule = new ActivityTestRule<>(DialogActivity.class);

    @Test
    public void testSomething() {
        // etc...
    }
}

My .travis.yml:

language: android
jdk: oraclejdk7
sudo: false
env:
  global:
    - ANDROID_API_LEVEL=23
    - BUILD_TOOLS_VERSION=23.0.3
    - ANDROID_ABI=armeabi-v7a
    - ADB_INSTALL_TIMEOUT=8
android:
  components:
    - tools
    - build-tools-$BUILD_TOOLS_VERSION
    - android-$ANDROID_API_LEVEL
    - add-on
    - extra
before_script:
  - echo no | android create avd --force -n test -t android-$ANDROID_API_LEVEL --abi $ANDROID_ABI
  - emulator -avd test -no-skin -no-audio -no-window &
  - android-wait-for-emulator
  - adb shell input keyevent 82 &
script: ./gradlew assembleMyappDebug connectedMyappDebugAndroidTest -PdisablePreDex

Update: Here are a few things that may be relevant in my build.gradle:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 23

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}
// ...
dependencies {
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.android.support:support-v4:23.1.0'
    compile 'com.google.android.gms:play-services-gcm:8.4.0'
    compile 'com.google.code.gson:gson:2.3'
    compile 'com.squareup.dagger:dagger:1.2.2'
    provided 'com.squareup.dagger:dagger-compiler:1.2.2'
    // ...
    androidTestCompile 'com.squareup:javawriter:2.5.0'
    androidTestCompile ('com.android.support.test.espresso:espresso-core:2.0') {
        exclude group: 'javax.inject'
    }
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
 }
How to solve:

I’m also new to android, but I`ll try to help you.

You can follow this answer:

https://groups.google.com/d/msg/adt-dev/QW5SuzQizNk/mWdaEuuCCQAJ

which links to:

https://github.com/google/dagger/issues/271

Basically this maybe the problem:

You might of used compile 'com.google.dagger:dagger-compiler:2.0.2' incorrectly.

The correct apt 'com.google.dagger:dagger-compiler:2.0.2'

and also check this link same as your issue

https://googleweblight.com/?lite_url=https://github.com/Shippable/support/issues/1453&ei=a6rzQlWF&lc=en-IN&s=1&m=140&host=www.google.co.in&ts=1471246688&sig=AKOVD67-05kCThujPFkmuySpywKlKpVG-g

###

There are 2 issues:

Issue#1:

com.android.builder.testing.ConnectedDevice > No tests
found.[test(AVD) – 6.0] FAILED

This is already described here in your case. This usually means that your test classes are not in the form that your test runner expects (e.g. don’t inherit from TestCase or lack @Test annotations).

For this you can run

gradlew :MyappAndroid:connectedMyappDebugAndroidTest

(where MyappAndroid is the directory name of the MyappAndroid module), which will only run your MyappAndroid‘s tests.

Resource Link:

https://stackoverflow.com/a/29717892/2293534

Issue#2:

This problem occurs for some dependency missing like dagger.

As artem-zinnatullin stated in the post

The problem is that you compile dagger-compiler to the project. I don’t want to investigate why it creates problems with instrumentation tests on Android 5+ but anyway, just do not compile it to the apk.

// probably test runner uses Dagger 2 internally and you break it via compiling own version, probably they even use Dagger 1, package names are same.

You can use APT Gradle Plugin https://bitbucket.org/hvisser/android-apt to run dagger-compiler during the compilation but do not compile it to the app.

Here is the #qualitymatters app with instrumentation tests and Dagger 2, everything works without problems, so you can take a look and see how to bake something similar.

For Gradle users, use https://github.com/tbroyer/gradle-apt-plugin.

For Maven users, use <optional>true</optional>.

Dagger documentation details are here: installation, gradle details

Resource Link:

https://stackoverflow.com/a/34332482/2293534

###

I had the same error but different testOptions and my project uses Dagger:

testOptions {
        execution 'ANDROIDX_TEST_ORCHESTRATOR' <---- remove this!
}

When I removed the execution line I stopped getting the error. Different bug but thought I’d share in case it can help anyone else!

Leave a Reply

Your email address will not be published. Required fields are marked *