JUnit can be setup within Android Studio with very little effort. In the end, I only had to add three lines to my build.gradle file and create a simple test class. Now my libGDX game has a testing framework that I can expand as it gets more complex.
As always, there was lots of googling involved, however this time the best answer actually came from google themselves. Here is their article on setting up unit tests.
JUnit Setup for my Android Studio Project
In the root directory of the project there is a filled called:
build.gradle
I made the following changes:
... ... @@ -23,6 +23,7 @@ allprojects { 23 23 box2DLightsVersion = '1.4' 24 24 ashleyVersion = '1.4.0' 25 25 aiVersion = '1.5.0' 26 + junitVersion = '4.12' 26 27 } 27 28 28 29 repositories { ... ... @@ -106,7 +107,9 @@ project(":core") { 106 107 compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion" 107 108 compile "com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion" 108 109 compile "com.badlogicgames.gdx:gdx-ai:$aiVersion" 110 + testCompile "junit:junit:$junitVersion" 109 111 } 112 + sourceSets.test.java.srcDirs = ["test/"] 110 113 } 111 114 112 115 tasks.eclipse.doLast {
The minor changes are as follows:
Line 26 goes with Line 110. I just added a variable for the junit version. I like how libGDX puts all of their versions in the same spot. It makes upgrading a lot easier.
The big changes are:
Line 110 makes the project depend on JUnit.
Line 112 makes the /src/test/ folder called “test”
In Android Studio, I switched to the “project” view, and then created a new folder “test”
The “test” folder shows up green (see the arrow in the image below) in Android Studio when done correct – and you are in the “project” view.
Under the “test” folder, I created a new package that matched my other package structure “com.stonemill.bubble”
Lastly, in the package, I created a “BubbleTest” class with the contents as follows:
BubbleTest.java
package com.stonemill.bubble; import org.junit.Assert; import org.junit.Test; public class BubbleTest{ @Test public void testMove() throws Exception { Assert.assertEquals(false, false); } }
This is a stub class for the move method of the Bubble class. I can not add test methods as I code.