Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add testCompile dependencies to IDE classpath

I have been struggling with unit-test-android problem for quite a long time. I have seen this, this and that, finally I found the gradle-android-test-plugin and even got it working. I can now run the tests with gradlew test command.

But, writing those tests in IDE (Android Studio or IntelliJ 13) is far from comfortable, because it does not see the junit & Robolectric dependencies added with testCompile dependency.

Is there any way to add these dependencies to the IDE classpath but still avoid to package them in the production app (thus, AFAIU compile dependency cannot be used)?

like image 452
fracz Avatar asked Nov 11 '22 16:11

fracz


1 Answers

I had the same problem with IntelliJ 14.1.3 today. The solution was to run the steps outlined here. Basically:

  1. Add JUnit and other dependencies via testCompile 'junit:junit:4.+', etz
  2. Put test sources in src/test/java/...
  3. To make the IDE find the test-dependencies (gradle will find them fine), open the "Build Variants"-view and set "Test Artifact" to "Unit Test". In "Project Structure", the testing dependencies should show up in your module with the "Test"-scope
  4. The commandline to run a test is testXxx, where Xxx is the build-type (debug/release/etz).

The important step here is the one in the "Build Variants" view. After you change it to "Unit Test", it will index and your libraries and full auto-completion are available.

like image 118
Lukas Knuth Avatar answered Nov 14 '22 22:11

Lukas Knuth