我在android studio中开始使用robolectric。 我会运行一个简单的例子,但我有这个错误
运行测试 测试运行开始了 junit.framework.AssertionFailedError:在com.example.myApp.AuthenticationActivityTest中找不到任何测试 在android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191) 在android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176) 在android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555) 在android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:1837)
@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"));
}
}
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
}
当我在gradle中的默认配置中添加testInstrumentationRunner时,如:
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
robolectic测试被认可,但我得到了这个错误:
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
答案 0 :(得分:0)
尝试添加;
sourceSets {
androidTest {
setRoot('src/test')
}
}
到你的build.gradle&#39; android&#39;关闭,针对您的测试目录。
您是否直接从Android Studio运行测试?我以前遇到过这个问题,尝试使用终端的gradle test命令运行它们。