Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio Cannot resolve symbol 'Before' in import org.junit.Before

I am using the junit-4.12 external library package in my Android Studio project. For some reason, when I import "import org.junit.Before;" into my java class I get the error "cannot resolve symbol 'Before'" (this is also happening with 'Test'), even though I can see that the files exists in my junit package (see image). I also have the dependency listed in my gradle (see 2nd image).

I noticed that someone else asked a similar question and the response was "Make sure that if JUnit is declared as < scope>test< / scope >, that your test class is in src/test, otherwise it won't be able to see the dependency."

Maybe this is my problem, but I am not sure how to make sure that my "test class is in src/test". (I am new to android studio... so sorry if this is a simple question)

"Before" and "Test" classes

Gradle Dependencies

like image 996
Natalie Avatar asked Mar 28 '16 22:03

Natalie


3 Answers

Using Android Studio 2.0 Beta 7

1)Go To Build > Clean Project.

2)In apps build.gradle add

dependencies{
    androidTestCompile 'junit:junit:4.12'
}

3)Steps :

1)Go To File > Project Structure.

2)Select your app from modules.

3)Go To Dependencies. Select junit:junit:4.12.

Select Test Compile.

4)Click Ok.

Import errors should get resolved.

like image 117
Harsh Shah Avatar answered Oct 26 '22 02:10

Harsh Shah


Be shure you are on Debug build variant. It doesn't work, if "Release" variant is chosen.

like image 27
DmitryBorodin Avatar answered Oct 26 '22 01:10

DmitryBorodin


In an Android project, the normal app code lives under app/src/main. The unit test code then lives under app/src/test, as you can see here:

This screenshot of the Android Studio project view shows the difference.

Notice that the test code area is highlighted in green. Also, to work with unit tests, you should also set your build variants Test Artifact to "Unit Tests".

Shown here

The screen here shows it on instrumentation tests, which is the default, so you you'll need to select unit tests from the dropdown.

like image 2
Doug Stevenson Avatar answered Oct 26 '22 03:10

Doug Stevenson