Adding JUnit to your Libgdx Project

This guide will show you how to add JUnit to an existing LibGdx project. The gradle version I’m using is 2.4.

In my case, it turned out to be pretty easy. Using an Android subproject makes this process more complex, please check the links below for more details!

1 – Add Test Dependencies to your Project build.gradle

Navigate to your build.gradle in the root project.


From there, add the following lines into your dependencies {} section.

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"

This will add jUnit and LibGdx to your testing environment.

2 – Create Test Folders

Now, let’s set up the basic folder structure required to launch tests:

  • src/main/java for the code you ship
  • src/test/java for test sources

LibGdx code by default is stored in src/ when you generate your project via the installer – so you need to move your non-test sources to src/main/java using the explorer (or whatever you prefer to use).

3 – Add Paths to your Core Project’s build.gradle

Next, you need to tell gradle where your main and test sources are. In your core build.gradle add the following lines: = [ "src/main/java/" ] = [ "src/test/java/" ]

EDIT: it looks like uncommenting = [ “src/” ] that exist when you edit the file does the job if that folder structure already exist.

That means 1) you don’t need those two lines per definition and 2) it seems to prevent tests to be detected, at least when I tried setting it up.

core gradle

Reload your project in your IDE. A working LibGdx JUnit setup with Gradle looks like this in Netbeans:

junit tests

Congratulations, you’re done!

To run your tests, run the project with the command gradle core:test.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s