我在android studio里开始使用robolectric。我会运行一个简单的例子,但是我有这个错误
运行测试测试运行已启动的junit.framework.AssertionFailedError:在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)的com.example.myApp.AuthenticationActivityTest中找不到测试
测试类
@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文件
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发布于 2015-03-24 19:06:17
尝试添加;
sourceSets {
androidTest {
setRoot('src/test')
}
}到你的build.gradle 'android‘闭包,指向你的测试目录。
另外,你是不是直接从Android Studio运行测试?我之前遇到过这样的问题,试着从终端使用gradle test命令运行它们。
https://stackoverflow.com/questions/29229607
复制相似问题