So you decided get some Unit tests running in JUnit except how the hell does one get started in libGDX in Android using Gradle. I pulled my hair out so hopefully you don't have to; here are a few steps I did which may help you move forward.
Create a directory where our tests will sit in
mkdir -p core/test/java/org/package/name
touch core/test/java/org/package/name/WorkingTest.java
The file core/src/test/java/org/package/name/WorkingTest.java
has this content for our example
package org.package.name;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class WorkingTest
{
@Test
public void thisAlwaysPasses()
{
assertTrue(true);
}
@Test
@Ignore
public void thisIsIgnored()
{
}
}
First we need to tell that junit is a dependency which needs to be compiled. Below shows a snippet of projects build.gradle
in the root folder:
...
project(":core") {
apply plugin: "java"
sourceSets.test.java.srcDirs = ["test/"]
dependencies {
compile "com.badlogicgames.gdx:gdx:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
compile fileTree(dir: 'libs', include: '*.jar')
testCompile "junit:junit:4.11"
testCompile "com.badlogicgames.gdx:gdx-backend-headless:$gdxVersion"
testCompile "com.badlogicgames.gdx:gdx:$gdxVersion"
testCompile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
testCompile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
testCompile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
}
}
...
The compile "junit:junit:4.11+"
is the line we added, note you can change this to junit:junit:+
to be more generic but the example above is what I have running
If you try to build for android you might get some APK LICENSE.txt
errors such as:
Duplicate files at the same path inside the APK: LICENSE.txt
To fix this we need to tell the android build process to exclude any files that have the name LICENSE.txt. To do this you can edit your android/build.gradle
file and add the packagingOption
option in the android
section. For example it should look like this:
android {
buildToolsVersion "19.0.3"
compileSdkVersion 19
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
packagingOptions {
exclude 'LICENSE.txt'
}
}
}
A simple
gradle :core:test
should work, try changing the assertTrue(true)
to assertTrue(false)
in core/src/test/java/org/package/name/WorkingTest.java
Let me know if you have any other problems, I might add them here to help other. Enjoy :D
6th Aug 2014
Glad it helps. :D
Really really nicely done man. worked like a bomb plus solved the issue I had with logging to stdout without spinning up the virtual machine.
Just putting this here incase anyone finds it helpful.
Enable logging to stdout. https://discuss.gradle.org/t/logging-to-console-and-file/8635
this line in your buld.gradle, doesn't have to exist in any task, just like you would an include:
logging.captureStandardOutput LogLevel.INFO
Now you can println out to the terminal or use your favourite logger.
This post contradicts itself. In one place, it says "core/test/java/org/package/name" and then in another place, it says, "core/src/test/java/org/package/name/".
Neither of those worked for me. What actually worked for me was, "core/test/org/package/name".