Robolectric 启动单个测试时无法正确加载

Robolectric fails to load properly when launching single test

我有一个 Android 应用程序,我使用 @RunWith(RobolectricTestRunner.class) 对其进行了测试。当我 运行 在 Android Studio 中一起测试时,它起作用了。但是当我 运行 测试或单独测试 class 时,我得到这个错误:

java.lang.UnsupportedOperationException: Robolectric does not support API level 1.

请注意,清单文件中指定了最低和目标 API 级别。好像环境没有设置好。添加 @Config(emulateSdk=23) 使测试开始,但在访问资源时测试失败。

知道什么会导致测试环境无法正确加载吗?

检查你的 gradle 和你的 class :

Gradle :

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

在你的测试中 Class :

@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 class 应该放在测试文件夹中(而不是 Android测试文件夹)

在 Android 工作室:

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