JUnit testing of libGDX game in Android Studio

Bringing JUnit4 into Android Studio to test your libGDX game

JUnit testing of libGDX game in Android Studio2

On first thought I assumed it would be a trivial endeavor to set up JUnit tests in Android Studio for libGDX-based game projects. But surprisingly enough, it turned out to be more challenging to say the least.

And the good news is that after a long, hard search through several articles, forums, and the Android Developer and Gradle websites, I came up with a simple procedure that worked well for me. Hopefully this will also help you avoid the time and effort I went through doing the research and working through the issues.

NOTE: This article is based on JUnit4 and Android Studio 2.1.2 (latest version at the time of this article) running on a Windows machine.


  1. Generate the libGDX game using the LibGDX Project Generator (gdx-setup.jar).
  2. Import the libGDX project into Android Studio according to one of my earlier articles: Set up libGDX game in Android Studio
  3. Follow the procedure in the “libGDX Core module” section below.
  4. Follow the procedure in the “libGDX Android module” section below that.

libGDX Core module

This procedure is for setting up JUnit4 within Android Studio 2 for unit testing the libGDX ‘core’ module.

Step 1: Set up the JUnit library

Add the JUnit library to the main build.gradle file (in the project root):

allprojects {
      ext {
        junitVersion = '4.12'

project(":core") {
    apply plugin: "java"

    dependencies {
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        testCompile "junit:junit:$junitVersion"

Step 2: Set up unit test folder

Since we’re using latest version of Android Studio (2.1.2 at the time of this writing), to get the best results we follow the latest Android developer recommended test folder structure: Test Types and Location. This will separate the unit test classes and any unit test resource files from the production classes and resource files.

So let’s first reorganize the directory structure. The ‘core’ module’s original and new libGDX directory structures are shown in this table:

Original New Description
core/src core/src/main/java Java sources directory
core/src/test/java Java unit test sources directory (where our unit test classes will reside)
core/src/test/res Optional: Unit test resources directory (for resource files that are only used by unit tests)
  1. Create the new directories according to the table above.
  2. Move the content from the old 'core/src' to the new 'core/src/main/java' location.

    NOTE: If you’re using Android Studio’s built-in version control system (VCS), then it should preserve the history of these folder changes for you. Otherwise, don’t forget to move the files in such a way that preserves your change history.

  3. Open the core/build.gradle and replace this line
sourceSets.main.java.srcDirs = [ "src/" ]

…with these lines.

sourceSets.main.java.srcDirs = [ "src/main/java/" ]
sourceSets.test.java.srcDirs = [ "src/test/java/" ]
sourceSets.test.resources.srcDirs = [ "src/test/res/" ]

Note: Include the resources line only if your tests will have their own resource files.

  1. Perform a Gradle project sync (click ‘Sync Now’ link if it automatically shows up in Android Studio).
  2. In the Project view, the 'core/src/test/java' folder should turn green indicating it is properly setup as a test folder recognized by Gradle and Android Studio.

WARNING: The creation of the Java folder might add the new ‘src/test/java/’ test directory as a source file to your build.gradle file’s sourceSets statement. So if you have the following entry in your app/build.gradle file, you need to remove it, since ‘test’ should not be treated as a normal source folder (like ‘main’ is)!

sourceSets { main { java.srcDirs = ['src/main/java', 'src/test/java/'] } }

Step 3: Create a test

    1. Open one of your source files to be tested (e.g. core/src/main/java/<my-package>/Utils.java)
    2. First click on the source file, then bring up the ‘Create Test’ dialog by:
      Ctrl + Shift + T > Create New Test…
    3. Make sure Testing library is set to: JUnit4
    4. Click ‘Fix’ link if it pops up.
    5. Change the Class name if desired.
    6. Leave the Destination package the same as the source file’s.
    7. Select the checkboxes of what to Generate.
    8. OK

Step 4: Edit the test (optional)

The test will pass even if we leave the test method empty. But you may optionally add an Assert statement like in this example unit test method that tests a numerical string conversion to a long:

public void testGetLong() throws Exception {
    Assert.assertEquals(123L, Utils.getLong("123"));

Step 5: Create a JUnit Run/Debug configuration

In order to execute the unit tests, create a JUnit Run/Debug configuration for a package of source files to test.

  1. In Project view, right-click on (green) 'core/src/test/java' folder and choose: Create ‘All Tests’
    Create a JUnit Run-Debug configuration
  2. The dialog pops up: Create Run/Debug Configuration: ‘All in core’
  3. Change the Name if desired.
  4. Click on the Package browse (…) icon and navigate to and select the desired package for this configuration to handle.Create Run-Debug Configuration All in core
  5. Run the new test configuration for the package: Run > Run ‘All in core’
  6. The Run view should open and display the test as passed.Run All in core

Author: TechDuke

Michael writes technical articles and is the editor for TechDuke.net. On the logical side, he is the founder and Principal Engineer for GameDuke Entertainment specializing in mobile and web application development. On the creative side, he is an artist, musician, and photographer skilled at creating and editing commercial graphics, music, sound effects, and videos.

Leave a Reply

Your email address will not be published. Required fields are marked *