Run JUnit tests in Gradle for Android app

I want to run simple, simple JUnit tests in my Android application project, using Gradle at the same time to write activity tests. It took some time to set up Gradle and get it working, but anyway, now I'm stuck trying to compile JUnit tests.

I checked this link, but when I started 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 not found ...

Below 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 
+8
android junit gradle
source share
2 answers

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

This is supported by Android 1.1+ and Android Gradle 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" } 

Read more here: Module Testing Support - Android Tools project site .

+9
source share

Look at this stackoverlfow thread: testing junit with gradle for android project

It looks like he is doing exactly what you need.

I think the problem in your script is that you did not declare

unitTest sourceset explicitly.

amuses Rene

0
source share

All Articles