当我尝试使用robolectric-rc2并运行以下测试时,我得到的是没有这样的android.content.res.resources$notfoundexception:no这样的标签com.yourapp.android:string/app_name。我尝试从manifest.xml文件中删除字符串,这些文件运行自定义运行并使用生成活动而不是安装活动。有什么想法吗?
@RunWith(TestRunner.class)
public class MainActivityTest {
private MainActivity mainActivity;
@Before
public void setUp() throws Exception {
mainActivity = Robolectric.setupActivity(MainActivity.class);
}
@Test
public void textTest() throws Exception {
TextView textView = (TextView) mainActivity.findViewById(R.id.daily_text);
assertEquals(textView, "com.yourapp.android");
assertThat(textView).isNotNull();
}}
我的毕业档案是这样的,我已经删除了一些图书馆。
configurations.all {
resolutionStrategy {
force 'org.hamcrest:hamcrest-core:1.3'
}
}
buildscript {
repositories {
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
mavenCentral()
}
}
allprojects {
repositories {
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
mavenCentral()
}
}
apply plugin: 'com.android.application'
apply plugin: 'org.robolectric'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
publishNonDefault true
defaultConfig {
applicationId "com.yourapp.android"
minSdkVersion 16
targetSdkVersion 21
versionCode 1
versionName "1.0"
multiDexEnabled = true
}
buildTypes {
debug {
debuggable true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
testCoverageEnabled true
}
release {
signingConfig signingConfigs.release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
unitTest
app
def versionNumber = "1.0"
googleFree {
applicationId "com.yourapp.android"
versionName versionNumber + "-googleFree"
}
}
sourceSets {
unitTest {
java {
srcDir 'src/test/java'
}
assets {
srcDirs = ['src/test/assets']
}
}
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LGPL2.1'
exclude 'META-INF/ASL2.0'
exclude 'LICENSE.txt'
exclude 'LICENSE'
}
lintOptions {
abortOnError false
}
dexOptions {
incremental true
javaMaxHeapSize "4g"
}
}
repositories {
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
mavenLocal()
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.2.1'
// Espresso
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
// Unit testing dependencies
unitTestCompile('junit:junit:4.12') { // Prevent duplication conflicts
exclude module: 'hamcrest-core'
exclude module: 'hamcrest-library'
exclude module: 'hamcrest-integration'
}
unitTestCompile 'org.hamcrest:hamcrest-core:1.1'
unitTestCompile 'org.hamcrest:hamcrest-library:1.1'
unitTestCompile 'org.hamcrest:hamcrest-integration:1.1'
unitTestCompile 'com.squareup.assertj:assertj-android:1.0.0'
unitTestCompile('org.robolectric:robolectric:3.0-rc3') {
exclude module: 'classworlds'
exclude module: 'commons-logging'
exclude module: 'httpclient'
exclude module: 'maven-artifact'
exclude module: 'maven-artifact-manager'
exclude module: 'maven-error-diagnostics'
exclude module: 'maven-model'
exclude module: 'maven-project'
exclude module: 'maven-settings'
exclude module: 'plexus-container-default'
exclude module: 'plexus-interpolation'
exclude module: 'plexus-utils'
exclude module: 'wagon-file'
exclude module: 'wagon-http-lightweight'
exclude module: 'wagon-provider-api'
}
}
// Roboletric config
robolectric {
// Configure includes / excludes
include '**/*Test.class'
include '**/*Tests.class'
exclude '**/espresso/**/*.class'
maxHeapSize = '2048m'
jvmArgs '-XX:MaxPermSize=512m', '-XX:-UseSplitVerifier'
maxParallelForks = 4
forkEvery = 150
ignoreFailures false
afterTest { descriptor, result ->
println "Executing test for ${descriptor.name} with result: ${result.resultType}"
}
}
//end Roboletric config
// Jacoco config
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.7.1.201405082137"
}
// Edit covered scope if needed
// For my part I like having the coverage of both application and tests
def coverageSourceDirs = [
'../app/src'
]
task jacocoTestReport(type: JacocoReport, dependsOn: "testUnitTestDebug") {
group = "Reporting"
description = "Generate Jacoco coverage reports"
classDirectories = fileTree(
dir: '../app/build/intermediates/classes',
excludes: ['**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/BuildConfig.*',
'**/Manifest*.*']
)
additionalSourceDirs = files(coverageSourceDirs)
sourceDirectories = files(coverageSourceDirs)
executionData = files('../app/build/jacoco/testUnitTestDebug.exec')
reports {
xml.enabled = true
html.enabled = true
}
}
//end Jacoco config
// Start build aliases
...
]
def expandedTaskList = []
gradle.startParameter.taskNames.each {
expandedTaskList << (buildAliases[it] ? buildAliases[it] : it)
}
gradle.startParameter.taskNames = expandedTaskList.flatten()
// end of build aliases
tasks.withType(Test).whenTaskAdded {
it.systemProperty 'com.yourapp.android', android.defaultConfig.applicationId // your package name
}
最佳答案
我已经用robolectrc3.0rc3修复了这个问题,并将下面的声明添加到我的所有unittest类中
@Config(constants = BuildConfig.class, sdk = 21)
@RunWith(RobolectricGradleTestRunner.class)
public class SomeRandomTest
{
...
}