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