1

I started with robolectric in android studio. I would run a simple example, but i have this error

Running tests Test running started junit.framework.AssertionFailedError: No tests found in com.example.myApp.AuthenticationActivityTest at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1837)

Test class

@RunWith(RobolectricTestRunner.class)
public class AuthenticationActivityTest {

@Test
public void checkAccountType() throws Exception{
    String accType = new AuthenticationActivity().getResources().getString(R.string.account_type);
    assertThat(accType,equalTo("com.example.myApp"));
}
}

Gradle file

apply plugin: 'com.android.application'
apply plugin: 'org.robolectric' //Robolectric

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.myApp"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            debuggable true
        }
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'LICENSE.txt'
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    androidTestCompile 'junit:junit:4.12' //Robolectric
    androidTestCompile 'org.robolectric:robolectric:2.4' //Robolectric
}

Update

when i add the testInstrumentationRunner in default config in the gradle like:

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

the robolectic tests become recognized, but i got this error:

com.example.myApp.AuthenticationActivityTest > testAccountType[small(AVD) - 5.0.2] FAILED 
java.lang.NullPointerException: parentLoader == null && !nullAllowed
at java.lang.ClassLoader.<init>(ClassLoader.java:210)
:app:connectedAndroidTest FAILED
houssine
  • 15
  • 6
  • Are you running them in Android Studio? Are you running them as instrumental tests (Android test)? – Eugen Martynov Mar 24 '15 at 15:30
  • I'm using Android studio. I try running them as android test and as junit but i get the same error. – houssine Mar 24 '15 at 18:26
  • I would say it is not possible to run `Robolectic` tests in Android Studio. I telling this because I don't know yet stable solution how to run tests with `android gradle` plugin v1.1.x and `Robolectric` inside the studio. So I would suggest you to concentrate on running tests from command line. Can you run `gradle clean testDebug` from the command line? – Eugen Martynov Mar 24 '15 at 19:41
  • gradle clear testDebug give BUILD SUCCESSFUL. – houssine Mar 24 '15 at 21:49
  • As expected. If it is enough for you I would close the question – Eugen Martynov Mar 24 '15 at 22:43
  • 1
    no i would say that the BUILD SUCCESSFUL, but during execution of test I get **Running tests Test running started junit.framework.AssertionFailedError: No tests found** – houssine Mar 24 '15 at 23:00
  • Did you read my comment about running tests from Android Studio? – Eugen Martynov Mar 24 '15 at 23:15
  • I run the test from the command line, but i get in the index.html : 0 tests, 0 failures ,0 ignored – houssine Mar 25 '15 at 09:22
  • Ah sorry, what is your folders structure? Can you move test to `src/test`. Please add suggestion from answer below so Android Studio will recognise test – Eugen Martynov Mar 25 '15 at 09:40
  • the tests class are placed in **src/androidTest/java/com.example.myApp**. I created a test that extends `ActivityInstrumentationTestCase2` (without robolectric) and it work (command : `gradlew connectedAndroidTest` ). I think that the problem is in my robolectric's config. – houssine Mar 25 '15 at 10:54
  • @houssine Please follow my answer here: http://stackoverflow.com/a/29188505/950427. If you are using `ActivityInstrumentationTestCase2`, keep those in `androidTest`, the Robolectric tests should be in `test`. – Jared Burrows Mar 25 '15 at 14:34
  • Adding on, you should be using Robolectric 3.0+ and `RobolectricGradleTestRunner`, you do not need to add `android.support.test.runner.AndroidJUnitRunner`. – Jared Burrows May 18 '15 at 17:16

1 Answers1

0

Try adding;

sourceSets {
    androidTest {
        setRoot('src/test')
    }
}

To your build.gradle 'android' closure, directed at your tests directory.

Also are you running the tests directly from Android Studio? I have had issues with this previously, try running them using the gradle test command from the terminal.

KimJongCusack
  • 141
  • 2
  • 6