iamreptar
iamreptar

Reputation: 1451

Android Studio with JUnit testing

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.

Upvotes: 3

Views: 8064

Answers (4)

Joe Birch
Joe Birch

Reputation: 371

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

Upvotes: 0

iamreptar
iamreptar

Reputation: 1451

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) ...

Upvotes: 1

Miguel El Merendero
Miguel El Merendero

Reputation: 2074

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" />

Upvotes: 0

IgorGanapolsky
IgorGanapolsky

Reputation: 26831

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

aidl.srcDirs = ['src']

Upvotes: 0

Related Questions