{"id":638,"date":"2015-07-29T05:09:43","date_gmt":"2015-07-29T00:09:43","guid":{"rendered":"http:\/\/www.stonemillkids.com\/?p=638"},"modified":"2016-10-03T22:49:17","modified_gmt":"2016-10-03T17:49:17","slug":"android-studio-setting-up-junit-testing","status":"publish","type":"post","link":"http:\/\/www.stonemillkids.com\/index.php\/2015\/07\/29\/android-studio-setting-up-junit-testing\/","title":{"rendered":"Android Studio &#8211; Setting up JUnit Testing"},"content":{"rendered":"<p>JUnit can be setup within Android Studio with very little effort.\u00a0 In the end, I only had to add three lines to my build.gradle file and create a simple test class.\u00a0 Now my libGDX game has a testing framework that I can expand as it gets more complex.<\/p>\n<p>As always, there was lots of googling involved, however this time the best answer actually came from google themselves.\u00a0 Here is their article on <a href=\"https:\/\/developer.android.com\/training\/testing\/unit-testing\/local-unit-tests.html#setup\" target=\"_blank\">setting up unit tests.<\/a><\/p>\n<h1>JUnit Setup for my Android Studio Project<\/h1>\n<p>In the root directory of the project there is a filled called:<\/p>\n<pre>build.gradle\r\n<\/pre>\n<p>I made the following changes:<\/p>\n<pre>...  ...  @@ -23,6 +23,7 @@ allprojects { \r\n23 \uf0e5  23           box2DLightsVersion = '1.4' \r\n24 \uf0e5  24           ashleyVersion = '1.4.0' \r\n25 \uf0e5  25           aiVersion = '1.5.0' \r\n  \uf0e5   26  +        junitVersion = '4.12' \r\n26 \uf0e5  27       } \r\n27 \uf0e5  28     \r\n28 \uf0e5  29       repositories { \r\n...  ...  @@ -106,7 +107,9 @@ project(\":core\") { \r\n106 \uf0e5  107           compile \"com.badlogicgames.gdx:gdx-box2d:$gdxVersion\" \r\n107 \uf0e5  108           compile \"com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion\" \r\n108 \uf0e5  109           compile \"com.badlogicgames.gdx:gdx-ai:$aiVersion\" \r\n  \uf0e5    110  +        testCompile \"junit:junit:$junitVersion\" \r\n109 \uf0e5  111       } \r\n  \uf0e5    112  +    sourceSets.test.java.srcDirs = [\"test\/\"] \r\n110 \uf0e5  113   } \r\n111 \uf0e5  114     \r\n112 \uf0e5  115   tasks.eclipse.doLast { \r\n<\/pre>\n<h4>The minor changes are as follows:<br \/>\nLine 26 goes with Line 110. I just added a variable for the junit version.\u00a0 I like how libGDX puts all of their versions in the same spot.\u00a0 It makes upgrading a lot easier.<\/h4>\n<h4>The big changes are:<br \/>\nLine 110 makes the project depend on JUnit.<br \/>\nLine 112 makes the \/src\/test\/ folder called &#8220;test&#8221;<\/h4>\n<h4>In Android Studio, I switched to the &#8220;project&#8221; view, and then created a new folder &#8220;test&#8221;<\/h4>\n<h4><img loading=\"lazy\" class=\"alignnone size-medium wp-image-909\" src=\"http:\/\/www.stonemillkids.com\/wp-content\/uploads\/2015\/07\/JUnit-Test-Package-in-Android-Studio-300x221.png\" alt=\"JUnit test folder in Android Studio\" width=\"300\" height=\"221\" srcset=\"http:\/\/www.stonemillkids.com\/wp-content\/uploads\/2015\/07\/JUnit-Test-Package-in-Android-Studio-300x221.png 300w, http:\/\/www.stonemillkids.com\/wp-content\/uploads\/2015\/07\/JUnit-Test-Package-in-Android-Studio-150x110.png 150w, http:\/\/www.stonemillkids.com\/wp-content\/uploads\/2015\/07\/JUnit-Test-Package-in-Android-Studio.png 522w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/h4>\n<p><img loading=\"lazy\" class=\"alignnone size-medium wp-image-910\" src=\"http:\/\/www.stonemillkids.com\/wp-content\/uploads\/2015\/07\/Setting-the-Project-View-in-Android-Studio-276x300.png\" alt=\"setting-the-project-view-in-android-studio\" width=\"276\" height=\"300\" srcset=\"http:\/\/www.stonemillkids.com\/wp-content\/uploads\/2015\/07\/Setting-the-Project-View-in-Android-Studio-276x300.png 276w, http:\/\/www.stonemillkids.com\/wp-content\/uploads\/2015\/07\/Setting-the-Project-View-in-Android-Studio-138x150.png 138w, http:\/\/www.stonemillkids.com\/wp-content\/uploads\/2015\/07\/Setting-the-Project-View-in-Android-Studio.png 335w\" sizes=\"(max-width: 276px) 100vw, 276px\" \/><\/p>\n<p>The &#8220;test&#8221; folder shows up green (see the arrow in the image below) in Android Studio when done correct &#8211; and you are in the &#8220;project&#8221; view.<\/p>\n<p>Under the &#8220;test&#8221; folder, I created a new package that matched my other package structure &#8220;com.stonemill.bubble&#8221;<\/p>\n<p>Lastly, in the package, I created a &#8220;BubbleTest&#8221; class with the contents as follows:<\/p>\n<p>BubbleTest.java<\/p>\n<pre>package com.stonemill.bubble;\r\n\r\nimport org.junit.Assert;\r\nimport org.junit.Test;\r\n\r\npublic class BubbleTest{\r\n    @Test\r\n    public void testMove() throws Exception {\r\n        Assert.assertEquals(false, false);\r\n    }\r\n}\r\n<\/pre>\n<p>This is a stub class for the move method of the Bubble class.\u00a0 I can not add test methods as I code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JUnit can be setup within Android Studio with very little effort.\u00a0 In the end, I only had to add three lines to my build.gradle file and create a simple test class.\u00a0 Now my libGDX game has a testing framework that I can expand as it gets more complex. As always, there was lots of googling [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20,15,19],"tags":[37,40,38,41,39],"_links":{"self":[{"href":"http:\/\/www.stonemillkids.com\/index.php\/wp-json\/wp\/v2\/posts\/638"}],"collection":[{"href":"http:\/\/www.stonemillkids.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.stonemillkids.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.stonemillkids.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.stonemillkids.com\/index.php\/wp-json\/wp\/v2\/comments?post=638"}],"version-history":[{"count":4,"href":"http:\/\/www.stonemillkids.com\/index.php\/wp-json\/wp\/v2\/posts\/638\/revisions"}],"predecessor-version":[{"id":911,"href":"http:\/\/www.stonemillkids.com\/index.php\/wp-json\/wp\/v2\/posts\/638\/revisions\/911"}],"wp:attachment":[{"href":"http:\/\/www.stonemillkids.com\/index.php\/wp-json\/wp\/v2\/media?parent=638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.stonemillkids.com\/index.php\/wp-json\/wp\/v2\/categories?post=638"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.stonemillkids.com\/index.php\/wp-json\/wp\/v2\/tags?post=638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}