【问题标题】:Using Roboletric for Junit testing使用 Roboletric 进行 Junit 测试
【发布时间】:2017-06-05 14:17:52
【问题描述】:

没有这样的清单文件:build/intermediates/bundles/debug/app/src/main/AndroidManifest.xml

参考-Robolectric says "AndroidManifest.xml not found"

https://github.com/robolectric/robolectric/issues/1648 但没有帮助。

【问题讨论】:

  • 你把测试课放在哪里了?你能打印一下你的项目结构吗?
  • 我的测试类写在app/src/test/java/com.packagename/MainActivityTest.

标签: java android unit-testing junit4 robolectric


【解决方案1】:

检查你的 gradle 和你的班级:

分级:

 testCompile 'org.robolectric:robolectric:3.3'
 testCompile 'junit:junit:4.12'
 testCompile 'org.assertj:assertj-core:1.7.1'

在你的测试类中:

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 25)
public class YourTest {

    @Test
    public void shouldNotBeNull() throws Exception {
       //put your test here for example
    }
)

YourTest 类应该放在 Test 文件夹中(不是 AndroidTest 文件夹)

在 Android Studio 上:

  • 编辑配置
  • 在 Junit 中,您必须将工作目录更改为 $MODULE_DIR$。 重要的是 $MODULE_DIR$。

【讨论】:

    【解决方案2】:

    您可以通过更新您的 gradle 文件来修复 No such manifest file: ./AndroidManifest.xml 警告。

    将以下行添加到您的 gradle 文件中,以便使用正确的 Android 清单。 includeAndroidResources 选项允许您在单元测试中访问 android 资源,包括您的 AndroidManifest 文件。 应用程序/build.gradle

    testOptions.unitTests {
        includeAndroidResources = true
    }
    

    src:https://codelabs.developers.google.com/codelabs/advanced-android-kotlin-training-testing-basics/index.html?index=..%2F..index#7

    【讨论】:

      猜你喜欢
      • 2017-05-12
      • 2023-04-11
      • 2014-05-28
      • 2012-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-19
      • 1970-01-01
      相关资源
      最近更新 更多