3

Android Studio: 3.6.1 Gradle: gradle-5.6.4-all

I am ready to test my app and found that Android Studio produces example unit and instrumented tests. So, I right-click ExampleUnitTest, select Run and it runs just fine. However, when I do this for ExampleInstrumentedTest, it fails with the following error message:

Process finished with exit code 1 Class not found: "com.example.jbiss.petminder.ExampleInstrumentedTest"

However, ExampleInstrumentedTest is located in the default androidTest path (...\app\src\androidTest\java\com\example\jbiss\petminder) and ExampleUnitTest is located in the default test path (...\app\src\test\java\com\example\jbiss\petminder).

While I altered the auto-generated Android Studio source to start experimenting with test coding, that should have nothing to do with the Class not found: "com.example.jbiss.petminder.ExampleInstrumentedTest" error. Here is my altered code (using the auto-generated ExampleInstrumentedTest code as the baseline):

package com.example.jbiss.petminder;

import android.content.Context;

import com.example.jbiss.petminder.activities.MainActivity;

import androidx.test.espresso.accessibility.AccessibilityChecks;
import androidx.test.filters.SmallTest;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.ActivityTestRule;

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

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static org.junit.Assert.*;

/**
* Instrumentation test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
@SmallTest
public class ExampleInstrumentedTest {

@Before
public void setUp(){
AccessibilityChecks.enable();
}

@Rule
public ActivityTestRule<MainActivity> mMainActivityActivityTestRule =
new ActivityTestRule<>(MainActivity.class);

@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

assertEquals("com.example.jbiss.petminder", appContext.getPackageName());
onView(withId(R.id.action_add_pet)).perform(click()).check(matches(withContentDescription(R.layout.activity_add_pet)));
}
}

Here is my app's build.gradle file:

apply plugin: 'com.android.application'

android {
compileSdkVersion 28
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
}
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "com.example.jbiss.petminder"
minSdkVersion 24
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

compileOptions {
encoding "UTF-8"
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

dataBinding {
enabled = true
}

// Gradle automatically adds 'android.test.runner' as a dependency.
useLibrary 'android.test.runner'

useLibrary 'android.test.base'
useLibrary 'android.test.mock'

testOptions {
unitTests.includeAndroidResources = true
}
}

dependencies {
def nav_version = "2.3.0-alpha03"

implementation "android.arch.navigation:navigation-fragment-ktx:1.0.0"
implementation "android.arch.navigation:navigation-ui-ktx:1.0.0"

// Java language implementation: navigation
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"

// Dynamic Feature Module Support
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"

// Testing Navigation
androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"

// use -ktx for Kotlin
implementation "android.arch.navigation:navigation-ui:$nav_version"

// use -ktx for Kotlin

//noinspection GradleCompatible
implementation 'com.android.support:support-v4:28.1.0'
implementation
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.android.support:design:28.1.0'
implementation 'com.android.support:cardview-v7:28.1.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'com.android.support:mediarouter-v7:28.1.0'

/**
* directly from https://developer.android.com/topic/libraries/architecture/adding-components#lifecycle
*/
def lifecycle_version = "2.2.0"
def arch_version = "2.1.0"
def room_version = "2.2.4"

// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
// Lifecycles only (without ViewModel or LiveData)
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"

// Saved state module for ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"

// Annotation processor
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
// alternately - if using Java8, use the following instead of lifecycle-compiler
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

// optional - helpers for implementing LifecycleOwner in a Service
implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"

// optional - ProcessLifecycleOwner provides a lifecycle for the whole application process
implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"

// optional - ReactiveStreams support for LiveData
implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version"

// optional - Test helpers for LiveData
testImplementation "androidx.arch.core:core-testing:$arch_version"

implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version"

// optional - Test helpers for LiveData
testImplementation "androidx.arch.core:core-testing:$arch_version"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"

// use kapt for Kotlin

// optional - RxJava support for Room

//implementation "androidx.room:room-rxjava2:$room_version"

// optional - Guava support for Room, including Optional and ListenableFuture

//implementation "androidx.room:room-guava:$room_version"

// Test helpers
testImplementation "androidx.room:room-testing:$room_version"
testImplementation 'org.testng:testng:6.9.10'

// Required -- JUnit 4 framework
testImplementation 'junit:junit:4.12'
// Optional -- Robolectric environment
testImplementation 'androidx.test:core:1.2.0'
// Optional -- Mockito framework
testImplementation 'org.mockito:mockito-core:2.19.0'
implementation 'androidx.appcompat:appcompat:1.1.0'

implementation 'com.google.api-client:google-api-client:1.30.2'

// Core library
androidTestImplementation 'androidx.test:core:1.2.0'

// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'

// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.ext:truth:1.2.0'
androidTestImplementation 'com.google.truth:truth:0.42'

// Espresso dependencies
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-web:3.2.0'
androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.2.0'

// The following Espresso dependency can be either "implementation"
// or "androidTestImplementation", depending on whether you want the
// dependency to appear on your APK's compile classpath or the test APK
// classpath.
androidTestImplementation 'androidx.test.espresso:espresso-idling-resource:3.2.0'
// debugImplementation because LeakCanary should only run in debug builds.
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.2'
// Optional -- Hamcrest library
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
// Optional -- UI testing with UI Automator
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'

}

configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
//if (requested.group == "androidx") {
if (!requested.name.startsWith("multidex")) {
details.useVersion '25.3.1'
}
}
}
}

While I might have other problems in my code, what would cause this failure to find an existing ExampleInstrumentedTest file with public class ExampleInstrumentedTest{} explicitly stated in that file?

The code seems to be formated as shown in Build instrumented unit tests

The following provided no answer:

Jeff
  • 431
  • 4
  • 16

3 Answers3

2

There's a mixing between androidx.test and android.support.test/android.test which is not advised. Changing the testInstrumentationRunner to the below could resolve the problem:

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

Here's a sample you could refer to for using androidx.test as the main testing framework.

ahasbini
  • 6,761
  • 2
  • 29
  • 45
  • unfortunately making that change didn't work, I get the same error message. I also checked [Set up project for AndroidX Test](https://developer.android.com/training/testing/set-up-project) and see that for step 3 of "Add Gradle dependencies" we're told to use 'android.test.' for **useLibrary**. I'm going to be looking over that sample later today. Thanks. – Jeff Mar 14 '20 at 14:56
  • I just created a new project and its automatically generated default ExampleInstrumentedTest successfully runs. Copying this code into the failing file did not work. – Jeff Mar 16 '20 at 22:32
  • Don't copy the code from your project to the new "clean" project, just run the test in the new project as is, the test should at least succeed with you. From there you could find the differences between the `build.gradle` file and the `ExampleInstrumentedTest` in the new project and your actual project to get them matching in some way to have a working test. – ahasbini Mar 17 '20 at 12:39
  • @ahabini, I copied and pasted as an experiment, just to see if there was some weird thing lurking in the failing instrumented test. When I compared both build.gradle files and changed 'def nav_version = "2.3.0-alpha03"' to 'def nav_version = "2.2.1"' (as in the "test" project) I get a _Could not determine the dependencies of task ':app:compileDebugRenderscript'._ error. The new "test" project's build.gradle is shorter, but I went through it to compare. – Jeff Mar 17 '20 at 15:59
  • I just wrote a brand new instrumented test (based on https://www.vogella.com/tutorials/AndroidTesting/article.html) and end up with 'Could not determine the dependencies of task _:app:processDebugAndroidTestManifest'._ error, among others. I can't find any information about these. Something seems very wrong with my app and I can't fix it. – Jeff Mar 17 '20 at 19:39
  • Based on my search from this https://www.google.com/search?q=android+%22Could+not+determine+the+dependencies+of+task%22 it seems that your app has reached a point of various incompatibilities in the libraries, plugins and build. If you could host your code so we could see it'll be a lot better, but in anyway I would try again and take it from the top, clean the `build.gradle` as much as possible, remove any dependencies to libraries (testing related or others), configuration, gradle plugins that are not related in **compiling** the app. Then try to add the testing from your preferred tutorial. – ahasbini Mar 17 '20 at 20:07
  • Also do note that you have `androidx` libraries used by your app, so use the testing libraries that have their names starting with `androidx` like here: https://developer.android.com/training/testing/set-up-project – ahasbini Mar 17 '20 at 20:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/209807/discussion-between-jeff-and-ahasbini). – Jeff Mar 17 '20 at 20:36
  • See my answer for a description of my ultimate findings as a result of ahasbini's suggestion. – Jeff Mar 18 '20 at 13:54
1

OK. from my discussion ahasbini, I found that I may have had some gradle issues. So, I made the following change to my build.gradle file:

From:

// Assertions
androidTestImplementation 'androidx.test.ext:truth:1.2.0'
androidTestImplementation 'com.google.truth:truth:0.42'

To:

// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.ext:truth:1.1.1'{
    exclude group: "com.google.truth", module: "truth"
}
androidTestImplementation 'com.google.truth:truth:0.44'{
   exclude group: "org.checkerframework", module: "checker-compat-qual"
   exclude group: "com.google.errorprone", module: "error_prone_annotations"
}

Now I'm getting the following error:

  • What went wrong: A problem occurred evaluating project ':app'.

    Could not find method androidx.test.ext:truth:1.1.1() for arguments [build_ezdi8oaa9gsnmfo7o2e18xyfx$_run_closure2$_closure11@6b8f394a] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

So, it appears that this is a problem with documentation in developer.android.com because I am following directions from its pages to develop my test code and Gradle dependencies tells me to use the following:

// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.0.0'
androidTestImplementation 'androidx.test.ext:truth:1.0.0'
androidTestImplementation 'com.google.truth:truth:0.42'

This code leads to the "not found" error. However, there is still a problem as there is nothing telling anyone to use that { exclude...} stuff nor what version of androidx.test.ext:truth to use to get functional code in the official documentation. Note that I use versions 1.1.1 because Gradle, in Android Studio, told that there was a "newer version available" for androidx.test.ext:junit.

NOTE: First, my Instrumented Test code uses NO Truth methods, so why this happens makes no sense. Second, I found Truth.dev when I was searching for current versions and tried to use what they said to use in "How to use Truth: Gradle" and it failed with the "Not found..." error.

Jeff
  • 431
  • 4
  • 16
0

OK. I found a work around to this. I created a new instrumented test file and copied the contents of the original problem file into it and changed the class name. It ran. I deleted the original file.

Jeff
  • 431
  • 4
  • 16