Getting error in ExampleInstrumentedTest.java file

10,347

Solution 1

It seems that you don't have added the Junit-Package to your build.gradle file for the module. Just add this in the dependencies block and it should work:

androidTestCompile 'junit:junit:4.12'
testCompile 'junit:junit:4.12'

Solution 2

When I create a new module from scratch, Android studio will produce a default gradle file with the following test related dependencies:

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})

This seems like it could solve at least some of the dependency issues in your build log. Since there is also a missing org.junit reference, possibly you also need to add junit as an androidTestCompile dependency, as @Chris already answered; or maybe it's implicitly included by espresso.

Solution 3

There may be missing dependencies in your gradle build add these to line :

testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:0.5'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2'

Solution 4

i migrated to android x and i found the error below in my ExampleInstrumentedTest.java class.

Error in ExampleInstrumentedTest.java

i changed the code to

    Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

which solved my error problem.

Share:
10,347
Abhishek Bhardwaj
Author by

Abhishek Bhardwaj

Updated on June 08, 2022

Comments

  • Abhishek Bhardwaj
    Abhishek Bhardwaj almost 2 years

    My project run successfully expect that when I clean and rebuild the project I getting this error

    Information:Gradle tasks [clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:compileDebugSources, :app:compileDebugAndroidTestSources, :app:compileDebugUnitTestSources]
    D:\Gonna Projects\DoctorApps\app\src\androidTest\java\in\doctormobileapps\doctorapps\ExampleInstrumentedTest.java
    Error:(4, 28) error: package android.support.test does not exist
    Error:(5, 35) error: package android.support.test.runner does not exist
    Error:(7, 17) error: package org.junit does not exist
    Error:(8, 24) error: package org.junit.runner does not exist
    Error:(11, 24) error: package org.junit does not exist
    Error:(18, 2) error: cannot find symbol class RunWith
    Error:(20, 6) error: incompatible types: Test cannot be converted to Annotation
    Error:(23, 30) error: cannot find symbol variable InstrumentationRegistry
    Error:Execution failed for task ':app:compileDebugAndroidTestJavaWithJavac'.
    > Compilation failed; see the compiler error output for details.
    Information:BUILD FAILED
    Information:Total time: 4.834 secs
    Information:9 errors
    Information:0 warnings
    Information:See complete output in console
    

    is there someone who can told me what's wrong i did?