5

This is my gradle file:

apply plugin: 'android'
apply plugin: 'android-test'

android {
// Check on it to know witch Android API level is necessary:
// http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels
compileSdkVersion 19
buildToolsVersion '19.0.1'

defaultConfig {
    minSdkVersion 14
    targetSdkVersion 19
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
buildTypes {
    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
}
sourceSets {
    androidTest {
        setRoot('src/test')
    }
}
// Patch: http://stackoverflow.com/questions/20673888/duplicate-files-copied-android-studio-0-4-0
packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/ASL2.0'
}
}

androidTest {
include '**/*Test.class'
exclude '**/espresso/**/*.class'
}

dependencies {

// Android SDK Extra librairies
compile 'com.android.support:support-v4:19.0.+'
compile 'com.android.support:appcompat-v7:19.0.+'
compile fileTree(dir: 'libs', include: ['*.jar'])

// Android testing
// http://robolectric.org/
androidTestCompile 'junit:junit:4.+'
androidTestCompile 'org.robolectric:robolectric:2.+'
androidTestCompile 'com.squareup:fest-android:1.0.+'
// had to deploy to sonatype to get AAR to work
//compile 'com.novoda:actionbarsherlock:4.3.2-SNAPSHOT'
} 

This my root gradle file:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
    maven { url "http://dl.bintray.com/populov/maven" }
    mavenCentral()
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}
dependencies {
    classpath 'com.android.tools.build:gradle:0.9.+'
    //classpath 'com.neenbedankt.gradle.plugins:android-apt:1.2+'
    //classpath 'com.novoda.gradle:robolectric-plugin:0.0.1-SNAPSHOT'
    classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.10.+'
}
}

allprojects {
repositories {
    maven { url "http://dl.bintray.com/populov/maven" }
    mavenCentral()
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
}

I just write an easy class to test to validate my infrastructure test, but I have this error:

Class not found: "com.example.myapp.activity.BaseActivityTest"

My test class is:

package com.example.myapp.activity;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

import static org.junit.Assert.*;

@RunWith(RobolectricTestRunner.class)
public class BaseActivityTest {
@Test
public void testTrueIsTrue() throws Exception {
    assertEquals(true, true);
}
}

My folder architecture is:

  • myapp

    • src

      • main

        • java
          • com.example.myapp ...
      • test

        • java
          • com.example.myapp ...

I don't understand why this error appears.

Federico Navarrete
  • 3,069
  • 5
  • 41
  • 76
anthony
  • 7,653
  • 8
  • 49
  • 101
  • Hey Anthony, I've got a working set-up for Android Studio 0.4.6 but know that later version of Android studio have caused problems - I asked [this question](http://stackoverflow.com/q/23116409/1348379) to a response of deafening silence! What version of AS are you running? – BrantApps May 19 '14 at 20:04
  • Hey OceanLife! It's the last version 0.5.5. Thanks! – anthony May 20 '14 at 08:03

1 Answers1

0

Ok guys, I found a solution:

This is my gradle file:

apply plugin: 'android'
apply plugin: 'robolectric'

android {
// Check on it to know witch Android API level is necessary:
// http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels
compileSdkVersion 19
buildToolsVersion '19.0.1'

defaultConfig {
    minSdkVersion 14
    targetSdkVersion 19
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
buildTypes {
    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
}
sourceSets {
    androidTest {
        setRoot('src/test')
    }
}
}

// prevent the "superClassName is empty" error for classes not annotated as tests
tasks.withType(Test) {
 scanForTestClasses = false
 include "**/*Test.class" // whatever Ant pattern matches your test class files
}

dependencies {

// Android SDK Extra librairies
compile 'com.android.support:support-v4:19.0.+'
compile 'com.android.support:appcompat-v7:19.0.+'
compile fileTree(dir: 'libs', include: ['*.jar', '*.aar'])

// Android testing
//androidTestCompile configurations.androidTestCompile.dependencies
// http://robolectric.org/
androidTestCompile 'org.robolectric:robolectric:2.+'
androidTestCompile 'junit:junit:4.+'
}

And I modified my VM argument (Configuration edition) like this post: http://blog.futurice.com/android_unit_testing_in_ides_and_ci_environments

anthony
  • 7,653
  • 8
  • 49
  • 101
  • I will never mix android + robolectric in same module. This exclude espresso (still works bit very slow), in AS I add to default junit task an extra external build step, so that AS generate the class files. Detail descriptions may be found here https://github.com/nenick/android-gradle-template/wiki/Tests-in-Android-Studio---IntellJ – nenick May 27 '14 at 03:39