【问题标题】:Android: Robolectric does not support API level 1Android:Robolectric 不支持 API 级别 1
【发布时间】:2014-05-26 09:47:29
【问题描述】:

这是我的基本测试类:

@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)

毕业信息:

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

谢谢你们!

【问题讨论】:

    标签: android junit4 robolectric


    【解决方案1】:

    有几种方法:

    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) 注释。

    第三种方法是最简单的方法,但您必须注释所有测试类。我个人更喜欢第二种方法,因为它在配置跑步者时提供了更大的灵活性。

    【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 从昨天开始还体验了以下工作:创建 robolectric.properties 文件(src/test/resources)并添加以下内容:sdk=21。在这种情况下,您不必在每个文件中都添加 @Config。
    【解决方案3】:

    Mac 用户注意事项

    如果您使用的是 Mac,您可能需要配置默认的 JUnit 测试运行程序配置,以解决 IntelliJ / Android Studio 未将工作目录设置为正在测试的模块的错误。这可以通过编辑运行配置、默认值 -> JUnit 并将工作目录值更改为 $MODULE_DIR$

    来完成

    设置:

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

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

    【讨论】:

    • 这也修复了它在 Ubuntu 14.04 上
    猜你喜欢
    • 2019-11-14
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    • 2013-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多