How to re-use the "test" task of the Java plugin in the Android plugin?

Hello,

I would like to use the Java “test” task from the Java plugin to a Gradle Android project.

The Android plugin version 0.3 doesn’t offer the “test” task and I was wondering if there is a way to reuse the java test task instead of writing a complete custom “test” task (and the depending tasks: compileTest, …) from scratch. Why: The actual android plugin version 0.3 doesn’t support unit testing, only instrument-test which require a device to run the test on. Some unofficial tools like Robolectric allow to do classic unit testing without the need to run the tests on a device or emulator.

Thanks a lot for Gradle, it’s really a wonderful tool.

Hi Nicolas,

You definitely can do this. It’s just a matter of creating a new task of type ‘Test’…

task unitTest(type: Test) {
 // Configure task in here
}

The docs for the test task are at: http://www.gradle.org/docs/current/dsl/org.gradle.api.tasks.testing.Test.html

At a minimum you need to configure:

  1. The test sources 2. The classpath

Without knowing your project structure it’s hard to give specific advice. I’d recommend having a look at those docs, giving it a go, then coming back with specific questions.

Hello,

I’ve tried to do this with 1.7, but I get a NullPointerException during task execution. My task is as follows:

task run(type: Test) {
    testClassesDir = file("tests")
    scanForTestClasses = true
    ignoreFailures = true
    classpath = files { file("lib").listFiles() }.plus( files{ testClassesDir } )
      useJUnit()
}

The exception I get when trying to run it is:

FAILURE: Build aborted because of an internal error.
  * What went wrong:
Build aborted because of an unexpected internal error. Please file an issue at: http://forums.gradle.org.
  * Try:
Run with --debug option to get additional debug info.
  * Exception is:
java.lang.NullPointerException
        at org.gradle.api.internal.project.taskfactory.OutputDirectoryPropertyAnnotationHandler$1.validate(OutputDirectoryPropertyAnnotationHandler.java:46)

If I apply the java plugin, everything works fine. Any idea what setting I’m missing?

I’m having the very same problem!!!

Somebody has a clue about it?

You need to set the ‘binResultsDirectory’ property.

I had the same problem in gradle 1.10. If you leave ’ binResultsDir’ unassigned, then the error is: ‘No value has been specified for property ‘binResultsDir’’ The null pointer exception in my case was caused by the fact that directories for Html and JUnit reports were not assigned. One can either assign directories:

reports.html.destination = ‘foo’

reports.junitXml.destination = ‘bar’

or disable reports:

reports.html.enabled = false

reports.junitXml.enabled = false