3

UPDATE

According to the new build system, the hierarchy should look like:

MyProject/
  src/
      androidTest/
          java/
              com.example.app.test/
                  ... (source code for tests) ...
      main/
          AndroidManifest.xml
          java/
            com.example.app/
                ... (source code for main application) ...
          res/
              ... (resources for main application) ...

However, I don't have access to the package private classes in the com.example.app package. The build.gradle android object looks like:

android {
    compileSdkVersion 19
    buildToolsVersion '19.0.1'

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
        testPackageName "com.example.app.test"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
        testFunctionalTest true
    }
}

I've also tried using the old structure, and get the error "Main Manifest missing from App/AndroidManifest.xml":

sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']
        resources.srcDirs = ['src']
        aidl.srcDirs = ['src']
        renderscript.srcDirs = ['src']
        res.srcDirs = ['res']
        assets.srcDirs = ['assets']
    }

    instrumentTest.setRoot('tests')
}

Do I need to use the old structure? What's missing from the new structure that would put the tests in scope of the com.example.app package?

UPDATE 2:

I'm seeing an error in the instrumentTest folders AndroidManifest.xml file: "Cannot resolve symbol 'com.example.app'" in the element

 <instrumentation
    android:name="android.test.InstrumentationTestRunner"
    android:targetPackage="com.example.app"/>

This is the test class:

package com.example.app.test;

import junit.framework.TestCase;

public class SimpleTest extends TestCase {

    public SimpleTest(String name) {
        super(name);
    }

    protected void setUp() throws Exception {
        super.setUp();
        assertTrue(true);
    }


    public void alwaysPasses() {

    }
}

I've also tried extending AndroidTestCase and received this error with both parent classes:

!!! JUnit version 3.8 or later expected:

java.lang.RuntimeException: Stub!
at junit.runner.BaseTestRunner.<init>(BaseTestRunner.java:5)
at junit.textui.TestRunner.<init>(TestRunner.java:54)
at junit.textui.TestRunner.<init>(TestRunner.java:48)
at junit.textui.TestRunner.<init>(TestRunner.java:41)
at com.intellij.rt.execution.junit.JUnitStarter.junitVersionChecks(JUnitStarter.java:185)
at com.intellij.rt.execution.junit.JUnitStarter.canWorkWithJUnitVersion(JUnitStarter.java:168)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

I've tried resolving the error with this solution but there is no Android 1.6 Platform in the Project Structure | Modules | Dependencies section. So, basically, I don't know how to move the junit dependency above the Android dependency in the classpath.

Community
  • 1
  • 1
iamreptar
  • 1,461
  • 16
  • 29
  • 1
    I would recommend reading http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Testing as a guide to testing under Gradle and then revising your question with anything you're still having problems with. – Scott Barta Feb 11 '14 at 22:01
  • Thank you for that resource! After reading and messing with the build.gradle file, there are still some bugs. I've updated the question as well. If there are any other resources that I should read then please let me know. – iamreptar Feb 11 '14 at 23:30
  • http://tools.android.com/tech-docs/unit-testing-support – Gelldur Mar 18 '15 at 11:25

4 Answers4

1

This problem was fixed by moving the test files into the same package that the (package-private) classes under test are in:

MyProject/
  src/
      androidTest/
          java/
              com.example.app/
                  ... (source code for tests) ...
      main/
          AndroidManifest.xml
          java/
            com.example.app/
                ... (source code for main application) ...
          res/
              ... (resources for main application) ...
iamreptar
  • 1,461
  • 16
  • 29
0

I had a similar issue, and I tracked the problem down to my gradle file. I needed the following in there:

aidl.srcDirs = ['src']
IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
0

The location for the test classes is already declared for Gradle in the inner .iml file of the Android Studio project. For example, as of 0.8.3 you can see:

      <sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
  <sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
  <sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
  <sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
  <sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
  <sourceFolder url="file://$MODULE_DIR$/src/androidTest/groovy" isTestSource="true" />
  <sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
  <sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
Miguel El Merendero
  • 2,054
  • 1
  • 17
  • 17
0

If you're still stuck with this I set up an example project that uses Espresso and Robotium test independently!

https://github.com/hitherejoe/Android-Boilerplate

Joe Birch
  • 371
  • 2
  • 7