安卓:Robolectric不支持API级别1。

20

这是我的基本测试类:

@RunWith(RobolectricTestRunner.class)
public class MainActivityTest {
  @Before
  public void setup() {
    //do whatever is necessary before every test
  }

  @Test
  public void testActivityFound() {
    Activity activity = Robolectric.buildActivity(MainActivity.class).create().get();

    Assert.assertNotNull(activity);
  }
}

当我在Android Studio下执行我的测试时,在终端窗口中,我遇到了这个错误:

java.lang.UnsupportedOperationException: Robolectric does not support API level 1, sorry!
at org.robolectric.SdkConfig.<init>(SdkConfig.java:24)
at org.robolectric.RobolectricTestRunner.pickSdkVersion(RobolectricTestRunner.java:320)
at org.robolectric.RobolectricTestRunner.getEnvironment(RobolectricTestRunner.java:296)
at org.robolectric.RobolectricTestRunner.access$300(RobolectricTestRunner.java:61)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:202)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)

Gradle 信息:

compileSdkVersion 19
buildToolsVersion "19.0.1"

    minSdkVersion 14
    targetSdkVersion 19
    versionCode 2

androidTestCompile 'org.robolectric:robolectric:2.+'
androidTestCompile 'junit:junit:4.+'

我使用最新的Android Studio版本:0.5.8

谢谢大家!

3个回答

28

有几种方法可以实现此目的:

  1. 在您的AndroidManifest.xml中指定targetSdkVersion
  2. 创建一个自定义的RobolectricTestRunner并覆盖getAppManifest方法以指定目标SDK版本。例如:

    public class MyCustomRunner extends RobolectricTestRunner {
    
        @Override
        protected AndroidManifest getAppManifest(Config config) {
    
            String manifestPath = "your_manifest_path";
            String resPath = "your_res_path";
            return new AndroidManifest(Fs.fileFromPath(manifestPath), Fs.fileFromPath(resPath)) {
                @Override
                public int getTargetSdkVersion() {
                    return 18;
                }
            }; 
        }
    }
    

    然后在您的MainActivityTest类中使用@RunWith(MyCustomRunner.class)

  3. 在您的MainActivityTest类顶部使用@Config(emulateSdk=18)注释。

第三种方法是最简单的,但您必须对所有测试类进行注释。我个人更喜欢第二种方法,因为它可以更灵活地配置您的运行器。


谢谢你,Andrei!完美的! - anthony
2
请阅读最后一条评论:https://github.com/robolectric/robolectric/issues/1831他们说:emulateSdk已被移除,需要使用constants=burrows.apps.example.template.BuildConfig,sdk=21。 - narancs

15

基于Andrei的答案,我尝试解决我的问题,但事实上我认为@Config(emulateSdk=18)已经被移除了,这就是解决我的问题的方法:

根据Andrei的回答,我尝试解决我的问题,但实际上我认为@Config(emulateSdk=18)已被删除,这是解决我的问题的方法:

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

1
从昨天的经验来看,以下方法也有效:创建robolectric.properties文件(src/test/resources),并添加以下内容:sdk=21。这样一来,您就不必在每个单独的文件中都添加@Config了。 - narancs

4

Mac 用户须知

如果您使用的是 Mac 操作系统,您可能需要配置默认的 JUnit 测试运行程序配置以解决 IntelliJ / Android Studio 未将工作目录设置为被测试模块的 bug。您可以通过编辑运行配置 Defaults -> JUnit 并将工作目录值更改为 $MODULE_DIR$ 来完成此操作。

设置:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class)
public class FooTest {
}

https://github.com/robolectric/robolectric/wiki/Running-tests-in-Android-Studio


1
这也在Ubuntu 14.04上解决了。 - Gus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接