2015-06-10 56 views
1

我得到沒有這樣的android.content.res.Resources $ NotFoundException:沒有這樣的標籤com.yourapp.android:string/app_name當我嘗試使用Robolectric-rc2並運行以下測試。我嘗試從運行自定義運行的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(); 
}} 

我的gradle文件看起來像這樣,我已經刪除了一些庫。

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 
} 

回答

0

它看起來像你的AndroidManifest文件中的以下行改變確實從

<activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 

在gradle這個文件

<activity 
     android:name=".MainActivity" 
     android:label="com.yourapp.android" > 

此外默認配置部分的伎倆能夠像這樣被註釋掉。

defaultConfig { 
    // applicationId "com.yourapp.android" 
    minSdkVersion 16 
    targetSdkVersion 21 
    versionCode 1 
    versionName "1.0" 
    multiDexEnabled = true 
} 
1

我用Robolectrci 3.0rc3與並添加以下聲明,以我所有的單元測試類

@Config(constants = BuildConfig.class, sdk = 21) 
@RunWith(RobolectricGradleTestRunner.class) 
public class SomeRandomTest 
{ 
... 
} 
+0

工作很好。爲什麼這有效的解釋? –

1

我有類似的問題,在我的項目,因爲我在構建有不同的applicationID固定這來自實際包裝的味道。在測試的config屬性中,在包屬性中指定實際的包層次結構。

@Config(sdk = 21, constants = BuildConfig.class, packageName = com.yourapp.android") 

此外,沒有必要爲unitTest flavor指定源目錄。當您運行測試用例時,它會被默認選中。只需指定資產目錄即可。試試這段代碼:

productFlavors { 
    unitTest { 
     sourceSets.main { 
      assets.srcDirs = ['src/test/assets'] 
     } 
    } 
} 

上述代碼將爲您的unitTest風格構建您的主文件夾中的所有資源。

相關問題