I am not able to run any JUnit test on a Gradle sub-project in Android Studio. This project does not rely on Android in any way, it uses only the Java Gradle plugin.
The Android Gradle project has the following folder structure:
settings.gradle lists the two sub-projects:
include ':app', ':backend'
The app folder contains an Android project. It's build.gradle file looks like this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.6'
}
}
apply plugin: 'android'
android {
compileSdkVersion 18
buildToolsVersion "18.0.1"
}
dependencies {
compile project(':backend')
}
The backend folder contains a normal Java Gradle project, which app depends on. It's build.gradle file looks like this:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.11'
}
The backend project has several unit tests located in src/test/java/... When I try to execute any of them, I get an error stating the Android Studio cannot find the test class:
Class not found: "com.test.DummyTest"
Process finished with exit code 1
Executing gradle test
works as expected. Is there any configuration I have missed, or is it plain impossible to get the unit tests to work in Android Studio?
JUnit is a “Unit Testing” framework for Java Applications which is already included by default in android studio. It is an automation framework for Unit as well as UI Testing. It contains annotations such as @Test, @Before, @After, etc.
By default, the source files for local unit tests are placed in module-name/src/test/ . This directory already exists when you create a new project using Android Studio.
Run Gradle testsIn your Gradle project, in the editor, create or select a test to run. From the context menu, select Run <test name>. icon in the left gutter. If you selected the Choose per test option, IntelliJ IDEA displays both Gradle and JUnit test runners for each test in the editor.
Support for running JUnit test in Java modules was added in version 0.3.6 of Android Studio: https://code.google.com/p/android/issues/detail?id=60916
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