首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Robolectric未找到测试

Robolectric未找到测试
EN

Stack Overflow用户
提问于 2015-03-24 18:01:24
回答 1查看 1.7K关注 0票数 1

我在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中找不到测试

测试类

代码语言:javascript
复制
@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文件

代码语言:javascript
复制
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时:

代码语言:javascript
复制
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

robolectic测试变得可识别,但我得到了这个错误:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2015-03-24 19:06:17

尝试添加;

代码语言:javascript
复制
sourceSets {
    androidTest {
        setRoot('src/test')
    }
}

到你的build.gradle 'android‘闭包,指向你的测试目录。

另外,你是不是直接从Android Studio运行测试?我之前遇到过这样的问题,试着从终端使用gradle test命令运行它们。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29229607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档