'Android Studio cannot resolve org.junit
I am using JUnit tests in Android Studio 1.2.2. The tests run without a problem. The only thing that puzzles me is that Android Studio cannot resolve the actual org.junit package. Obviously it does resolve it otherwise the tests wouldn't run. But the import and annotations are marked red in Android Studio as shown here. Is this a bug in Android Studio? I tried rebuilding the app and restarting Android Studio but this doesn't correct the problem.
import org.junit.Test;
import static org.junit.Assert.*; // cannot resolve symbol 'junit'
public class CanadaTest
{
@Test
public void testCountryName() throws Exception
{
int x = 0;
x++;
}
}
Solution 1:[1]
Try File -> Invalidate Caches / Restart
.
Solution 2:[2]
Put this in your gradle file:
testCompile 'junit:junit:4.12'
Solution 3:[3]
If none of the above work, it might be that your test classes are in the wrong directory.
I ran into this issue because my text classes were in the /main/java
dir instead of the /test
dir where they're supposed to be...
In short:
Make sure your test classes are in the /test
directory!
Solution 4:[4]
I had this problem and it turned out that it was just due to my having left my build variant set to "release". Once I changed it back to "debug", Android Studio did a build and when it finished the red errors on junit annotations went away. pic of build variant setting in android studio
Solution 5:[5]
I resoleved it by combination of some answers and some changes:
1.Make sure to set prepare targetSdkVersion
, compileSdkVersion
and buildToolsVersion
.
2.I used the last Android Gradle Plugin Version
and Gradle Version
.
3.Add some lines to buil.gradle on app level(I mark them with '\*' ):
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
defaultConfig {
applicationId "com.zima.testapp"
minSdkVersion 22
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" //*
apply plugin: 'maven' //*
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
dependencies {
testImplementation 'junit:junit:4.12' //*
implementation 'junit:junit:4.12' //*
androidTestImplementation 'com.android.support:support-annotations:28.0.0' //*
androidTestImplementation 'com.android.support.test:runner:1.0.2' //*
androidTestImplementation 'com.android.support.test:rules:1.0.2' //*
implementation fileTree(dir: 'libs', include: ['*.jar']) //*
implementation fileTree(dir: 'libs', include: 'Parse-*.jar') //*
}
}
4.build.gradle on project level is like this:
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
apply plugin: 'java' //*
}
}
allprojects {
repositories {
jcenter()
google()
maven {
url "https://jitpack.io"
}
}
}
Solution 6:[6]
Problem
Classes and annotations of the org.junit.* were not found in the instrumentation tests (androidTest). These classes wer found in the junit tests (test).
Solution:
Switching the Build Variant from Release to Debug.
Solution 7:[7]
May be in your gradle file you have something like this:
configurations {
debugImplementation.exclude group: "junit", module: "junit"
}
After removing everything will work.
Solution 8:[8]
If you've reinstalled android studio remember by default it is not using your systems JDK (Project structure -> SDK location -> jdk location).
Solution 9:[9]
change junit
to the latest version
testImplementation 'junit:junit:'
to
testImplementation 'junit:junit:4.13.2'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow