title: How to set up unit tests for Android / Gradle
body: <p>After having read the <a href="http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Testing">android guide on testing</a> with the android gradle plugin, I wanted to set up JUnit tests for my POJO's that don't run with the instrumented tests. The idea was that tests for code that doesn't depend on Android should be very fast (and facilitate TDD).</p>
  
  <p>Is there a standard way to set up a source set and task in build.gradle to accomplish this? That is the main question, the secondary question is what's wrong with my attempt below...</p>
  
  <p>I'm using Android Studio 0.4.2 and Gradle 1.9, experimenting with a simple JUnit test class in a new "test" folder. Here is what I have so far, but when I run "gradle testPojo" I get this result:</p>
  
  <pre><code>:android:assemble UP-TO-DATE
  :android:compileUnitTestJava UP-TO-DATE
  :android:processUnitTestResources UP-TO-DATE
  :android:unitTestClasses UP-TO-DATE
  :android:testPojo FAILED
  
  * What went wrong:
  Execution failed for task ':android:testPojo'.
  &gt; failed to read class file 
  /path/to/project/android-app/build/classes/unitTest/TestClass.class
  </code></pre>
  
  <p>I verified that the class is in fact there, so I'm confused as to why the task is not able to read the file.</p>
  
  <p>Here is the build.gradle file:</p>
  
  <pre><code>...
  
  sourceSets {
    unitTest {
      java.srcDir file('src/test/java')
      resources.srcDir file('src/test/resources')
    }
  }
  
  dependencies {
    ...
    unitTestCompile 'junit:junit:4.11'
  }
  
  configurations {
    unitTestCompile.extendsFrom instrumentTestCompile
    unitTestRuntime.extendsFrom instrumentTestRuntime
  }
  
  task testPojo(type: Test, dependsOn: assemble){
    description = "Run pojo unit tests (located in src/test/java...)."
    testClassesDir = sourceSets.unitTest.output.classesDir
  
    android.sourceSets.main.java.srcDirs.each { dir -&gt;
      def buildDir = dir.getAbsolutePath().split('/')
      buildDir =  (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/')
      sourceSets.unitTest.compileClasspath += files(buildDir)
      sourceSets.unitTest.runtimeClasspath += files(buildDir)
    }
    classpath = sourceSets.unitTest.runtimeClasspath
  }
  
  check.dependsOn testPojo
  </code></pre>