I am trying to get tests ( junit and robolectric ) working in an Android project but am totally stuck. My main problem is that all testing I found with gradle somehow pull in the java plugin and then I get this error:
The 'java' plugin has been applied, but it is not compatible with the Android plugins.
The only way out I see at the moment is to split into test and app project - but I would like to avoid that. Any examples/hints would be highly appreciated!
In the official documentation there is no mention of unit-testing - only Instrumentation-Tests - but I want unit-tests to get results fast.
In 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.
Enabling the Gradle's Native JUnit 5 SupportEven though Gradle (version 4.6 or newer) has a native support for JUnit 5, this support isn't enabled by default. If we want to enable it, we have to ensure that the test task uses JUnit 5 instead of JUnit 4.
You can do gradle -Dtest. single=ClassUnderTestTest test if you want to test single class or use regexp like gradle -Dtest. single=ClassName*Test test you can find more examples of filtering classes for tests under this link.
A Gradle plugin that allows for the execution of JUnit 5 tests in Android environments using Android Gradle Plugin 4.0. 0 or later.
You don't need the Java plugin, since the Android will take care of what you need mostly, from what I've seen so far.
I managed to get my Robolectric and junit tests running via this man's blog: http://tryge.com/2013/02/28/android-gradle-build/
My build.gradle file looks like this (where my test files are in the {projectdir}/test directory.
...
// Unit tests
sourceSets {
unitTest {
java.srcDir file('test')
resources.srcDir file('test/resources')
}
}
dependencies {
unitTestCompile files("$project.buildDir/classes/debug")
unitTestCompile 'junit:junit:4.11'
unitTestCompile 'org.robolectric:robolectric:2.1.1'
unitTestCompile 'com.google.android:android:4.0.1.2'
}
configurations {
unitTestCompile.extendsFrom runtime
unitTestRuntime.extendsFrom unitTestCompile
}
task unitTest(type:Test, dependsOn: assemble) {
description = "run unit tests"
testClassesDir = project.sourceSets.unitTest.output.classesDir
classpath = project.sourceSets.unitTest.runtimeClasspath
}
build.dependsOn unitTest
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.
This guide might help - http://www.slideshare.net/tobiaspreuss/how-to-setup-unit-testing-in-android-studio
Latest gradle the test should be under androidTest dir
Also in your gradle.build:
dependencies {
androidTestCompile 'junit:junit:4.+'
}
also add those under defaultConfig {
testPackageName "test.java.foo"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
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