This problem happens because Android Platform (android.jar
) already contains JUnit classes. IDEA test runner loads these classes and sees that they are from the old JUnit, while you are trying to use annotated tests which is a feature of the new JUnit, therefore you get the error from the test runner.
The solution is simple, open the Project Structure
| Modules
| Dependencies
, and move the junit-4.7.jar
up, so that it comes before Android 1.6 Platform
in the classpath. Now the test runner will be happy as it loads the new JUnit version.
my module is a java library module, so changing JRE to 1.8 java solved the issue.
Or, you can also do it globally via Module Settings > SDK Location > JDK, specifying Oracle's JDK 8 instead of Android SDK's copy.
I had this problem with a multi module project (libgdx). One module is pure Java and has tests. My solution was to set "use alternative JRE" to "Java 1.8" in the run configuration of my unit tests. This makes sure no android.jar is on the classpath and the junit 4.x runner is used.
I got the same error when creating both Unit Test
and Android Instrument Test
in Android Studio 1.4+ and it started to get confused. To avoid this error make sure your test class is fall under Android Tests
on Run/Debug Configurations
Test Artifact
in Build Variants
is set to Android Instrumentation Tests
Run
> Edit Configuration
Android Tests
instead of JUnit
JUnit
simply delete the config and right click on the file you want to test and Run
again. It will then create the config under Android Tests
section and it run on device/emulator.For Android Studio - starting from Android Studio 1.1 Beta 4, Google has added support for Android Gradle plugin 1.1.0-RC. The new plugin supports Unit Testing through Android Studio using junit 4+.
This is still experimental and there are some manual steps to set this up.
For everyone who is reading this post and still have the same issue with AndroidStudio 1.0. You cannot change the dependency order in AndroidStudio has the IDE re-write them automatically. And, even if you manage to change the order by modifying the .iml file, you will get a "class not found...". This is because the Test output path cannot be set on AndroidStudio.
Actually, there is solution to make AndroidStudio, Junit and Robolectric working together. Take a look at this https://github.com/JCAndKSolutions/android-unit-test and use this plugin as well : https://github.com/evant/android-studio-unit-test-plugin
Works perfectly for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With