With IntelliJ, I could create a directory "test", add some POJO test files and run them through the IDE.
With Android Studio, I can't find a way to do that. I tried to modify the build.gradle, but I always get the class not found exception.
The next step would be to set up a rule that loads some Guice bindings only during test.
My project looks like:
Project
Module
LibraryModule
src
main
java/com/temp/...
test
java/com/temp/TestClass.java
TestClass.java:
package com.temp;
import org.junit.Before;
import org.junit.Test;
public class TestClass {
@Before
public void setup() throws Exception {
}
@Test
public void testSomething() {
//do something
}
}
build.gradle:
sourceSets {
unitTest {
java.srcDir file('src/test/java')
resources.srcDir file('src/test/resources')
}
}
dependencies {
instrumentTestCompile fileTree(dir: 'libs/test', include: '*.jar')
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.android.support:support-v4:13.0.0'
}
Error I get:
Class not found: "com.temp.TestClass"
When I run the same project in IntelliJ, I don't get any error. Any tips would help, thanks!
Complete answer (based on what I found here: http://tryge.com/2013/02/28/android-gradle-build/)
build.gradle:
sourceSets {
unitTest {
java.srcDir file('src/test/java')
resources.srcDir file('src/test/resources')
}
}
configurations {
unitTestCompile.extendsFrom runtime
unitTestRuntime.extendsFrom unitTestCompile
}
dependencies {
unitTestCompile files("$project.buildDir/classes/release")
}
task unitTest(type: Test, dependsOn: assemble) {
description = "run unit tests"
testClassesDir = project.sourceSets.unitTest.output.classesDir
classpath = project.sourceSets.unitTest.runtimeClasspath
}
check.dependsOn unitTest
dependencies {
unitTestCompile fileTree(dir: 'libs/test', include: '*.jar')
unitTestCompile fileTree(dir: 'libs', include: '*.jar') // redundant, but don't know how to fix it
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.android.support:support-v4:13.0.0'
}
Then, to add the test bindings for Guice, make your test class extends that:
public class PojoTestCase {
public PojoTestCase() {
Guice.createInjector(new TestBindings()).injectMembers(this);
}
}
And the way to run:
./gradlew unitTest
Also, the test won't run from Android Studio, but I'm fine with that for now.