Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running unit tests on a Java sub-project in Android Studio

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
  • app/build.gradle
  • backend/build.gradle

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?

like image 639
bjorncs Avatar asked Sep 13 '13 22:09

bjorncs


People also ask

Which Java framework is used to write unit tests in android?

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.

Which directory is used to store unit tests in your Android project?

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.

How do I run a JUnit test case in Gradle?

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.


1 Answers

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

like image 83
bjorncs Avatar answered Oct 13 '22 22:10

bjorncs