Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error in ExampleInstrumentedTest.java file

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?

like image 549
Abhishek Bhardwaj Avatar asked Oct 14 '16 12:10

Abhishek Bhardwaj


4 Answers

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'
like image 148
Chris Avatar answered Nov 08 '22 12:11

Chris


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.

like image 28
JHH Avatar answered Nov 08 '22 12:11

JHH


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.

like image 1
Abdul Wahid Avatar answered Nov 08 '22 14:11

Abdul Wahid


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'
like image 2
David Kathoh Avatar answered Nov 08 '22 12:11

David Kathoh