Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run JUnit tests in Gradle for Android app

I want to run simple, plain JUnit tests in my Android app project, using Gradle at the same time to write Activity tests afterwards. It took a loong time to configure Gradle and make it work, but, anyway, now I'm stuck trying to make JUnit tests just compile.

I checked this link, but when I run gradle I get the following error:

DummyTest.java:3: error: package junit.
framework does not exist
import junit.framework.Assert;
                      ^
\DummyTest.java:8: error: cannot find symbol
        Assert.assertEquals(5,3);
        ^
  symbol:   variable Assert
  location: class DummyTest

So, junit is not found...

The following is my full gradle.build file:

    buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.6.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

dependencies {
    compile files('libs/joda-time-2.3.jar')
    compile files('libs/android-support-v4.jar')
    unitTestCompile files("$project.buildDir/classes/release")
    unitTestCompile 'junit:junit:4.8.2'
}

android {
    compileSdkVersion 17
    buildToolsVersion '17.0.0'

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
        }

        unitTest {
            java.srcDir file('test')
            resources.srcDir file('test/res')
        }
    }
    defaultConfig {
        minSdkVersion 14
        versionName '1.0'
        versionCode 1
        targetSdkVersion 17
    }
}

// add the unitTest task
task unitTest(type:Test, dependsOn: assemble) {
    description = "run unit tests"
    testClassesDir = project.sourceSets.unitTest.output.classesDir
    classpath = project.sourceSets.unitTest.runtimeClasspath
}

build.dependsOn unitTest
like image 299
Luis Avatar asked Nov 23 '13 10:11

Luis


People also ask

How does Gradle run JUnit tests?

Test detection By default, Gradle will run all tests that it detects, which it does by inspecting the compiled test classes. This detection uses different criteria depending on the test framework used. For JUnit, Gradle scans for both JUnit 3 and 4 test classes.

Does Gradle use JUnit?

The junit-jupiter dependency is an aggregator artifact which simplifies the dependency management because it ensures that the required dependencies are found from the classpath. Gradle has a native support for JUnit 5, but this support isn't enabled by default.

How do you write JUnit test cases for Android?

You write your local unit test class as a JUnit 4 test class. To do so, create a class that contains one or more test methods, usually in module-name/src/test/ . A test method begins with the @Test annotation and contains the code to exercise and verify a single aspect of the component that you want to test.


1 Answers

AndroidStudio and the new Android Gradle plugin are now offering official unit test support.

This is supported from Android Studio 1.1+ and Android Gradle plugin version 1.1.0+

Dependencies can now be declared as testCompile:

dependencies {
  testCompile 'junit:junit:4.12'
  testCompile "org.mockito:mockito-core:1.9.5"
}

More details here: Unit testing support - Android Tools Project Site.

like image 70
unbekant Avatar answered Nov 02 '22 09:11

unbekant