Android:Robolectric不支持API级别1

ant*_*ony 20 android junit4 robolectric

这是我的基础测试类:

@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);
  }
}
Run Code Online (Sandbox Code Playgroud)

当我在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)
Run Code Online (Sandbox Code Playgroud)

Gradle信息:

compileSdkVersion 19
buildToolsVersion "19.0.1"

    minSdkVersion 14
    targetSdkVersion 19
    versionCode 2

androidTestCompile 'org.robolectric:robolectric:2.+'
androidTestCompile 'junit:junit:4.+'
Run Code Online (Sandbox Code Playgroud)

我使用上一个Android Studio版本:0.5.8

感谢你们!

And*_*ean 28

有几种方法:

  1. 在您AndroidManifest.xml的指定targetSdkVersion
  2. 创建用于指定目标sdk 的自定义RobolectricTestRunner和覆盖getAppManifest方法.例如:

    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;
                }
            }; 
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    然后在你的MainActivityTest课堂上使用它@RunWith(MyCustomRunner.class).

  3. @Config(emulateSdk=18)MainActivityTest课堂上使用注释.

第三种方法是最简单的方法,但您必须注释所有测试类.我个人更喜欢第二种方法,因为它可以更灵活地配置跑步者.

  • 请阅读最后一条评论:https://github.com/robolectric/robolectric/issues/1831主题:emulateSdk已删除,需要使用constants = burrows.apps.example.template.BuildConfig,sdk = 21 (2认同)

Kar*_*oly 15

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

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class
     , sdk = 21)
   public class SplashActivityTest {
  ......
  }
Run Code Online (Sandbox Code Playgroud)