Search code examples
unit-testingandroid-studiolibgdx

Is it possible to do unit testing with libgdx and Android Studio 1.1?


I'm a complete newb when it comes to Gradle, so I've been stuck trying to get it to work. I've tried following several guides but they tell me different things (e.g. use instrumentTest, androidtest, test, etc.).

I've managed to get the folder to show up highlighted in green, but then this appears.

Execution failed for task ':android:mockableAndroidJar'.
> java.lang.IllegalArgumentException (no error message)

I've tried Robolectric as well but I have no idea why nothing is working. And of course, since Android Studio 1.1 is relatively new (which supports unit testing), help is rather scarce.


Solution

  • Here is how i proceeded to use Junit tests for my libgdx project in Android Studio :

    Step 1 : Enable test folder and setup the gradle project for unit testing

    In Android Studio, it will be shown as a green folder when it's correctly setup. To setup folder test as a test folder, here is what i did in my core libgdx gradle project :

    project(":core") {
        apply plugin: "java"
    
        sourceSets.test.java.srcDirs = ["test/"]
    
        dependencies {
            compile "com.badlogicgames.gdx:gdx:$gdxVersion"
            compile "com.badlogicgames.gdx:gdx-ai:$aiVersion"
            ...
    
            // Test dependencies :
            testCompile "junit:junit:4.11"
            testCompile "org.mockito:mockito-core:1.9.5"
        }
    }
    

    Now the test folder is setup and the test framework should be available in the test folder. You may have to create the test folder manually if it doesn't exists if i remember well.

    enter image description here

    Step 2 : Enable unit testing in Android Studio 1.1

    The option to enable unit testing tools in Android studio 1.1 is a bit hidden as the feature is still considered as experimental. To enable it, go to Settings/Gradle and check the box to enable the feature.

    enter image description here

    Step 3 : Run some tests

    Right click on your test folder and select "Run all tests" and this nice views displaying test's results shoud appear.

    enter image description here

    I hope that helps.