• Top
  • Comment
  • Reply

JUnit with libGDX using Gradle

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 the directory and a test file

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 
    public void thisAlwaysPasses() 

    public void thisIsIgnored() 

Setting up Gradle

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

Ignoring some files which are conflicting when building

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']


        packagingOptions {
            exclude 'LICENSE.txt'

Running tests

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


11th Jul 2014
© 2011 Shahmir Javaid - http://shahmirj.com/blog/38

perfect that is what I was looking for. I did put the sourceSets.test.java.srcDirs to src/.

And it looks like the testCompile "com.badl***" dependencies are not required?

maybe this is cause i test with ./gralew core:test

Shahmir Javaid

6th Aug 2014

Glad it helps. :D

Daniel Gerson

23rd Aug 2014


david buttar

5th Feb 2015

sup playa

Joao Nogueira

6th Jun 2015

Really very helpfull! This quickly solved an issue I had for some time and couldn't find an answer to it!

Thank you, this also works with ./gradlew :core:test

Charl Mert

24th Jul 2015

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.

Vinoth Gebrial

19th Apr 2016

so is the test folder in the src folder or in the core folder? First you make the folder and the file in the core folder, then when you show what's in the file you say it is in the src folder

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".

Back to Top
All content is © copyrighted, unless stated otherwise.
Subscribe, @shahmirj, Shahmir Javaid+