Android Robolectric 是否支持 API 级别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20541630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:00:26  来源:igfitidea点击:

Does Robolectric support API level?

androidrobolectric

提问by Kitesurfer

I have some Test which I would like to run with Robolectric, I use the 2.3-SNAPSHOT as my APP uses the ActionbarCompat i needed to use 2.3-SNAPSHOT Version as Robolectric could not find the AppCompat Themes before. So I setup the Classpath in Eclipse and I end up with this:

我有一些我想用 Robolectric 运行的测试,我使用 2.3-SNAPSHOT,因为我的应用程序使用 ActionbarCompat 我需要使用 2.3-SNAPSHOT 版本,因为 Robolectric 之前找不到 AppCompat 主题。所以我在 Eclipse 中设置了类路径,最后得到了这个:

java.lang.UnsupportedOperationException: Robolectric does not support API level 9, sorry!
at org.robolectric.SdkConfig.<init>(SdkConfig.java:24)
at org.robolectric.RobolectricTestRunner.pickSdkVersion(RobolectricTestRunner.java:288)
at org.robolectric.RobolectricTestRunner.getEnvironment(RobolectricTestRunner.java:264)
at org.robolectric.RobolectricTestRunner.access0(RobolectricTestRunner.java:57)
at org.robolectric.RobolectricTestRunner.evaluate(RobolectricTestRunner.java:186)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.vendor.test" 
      android:versionCode="1"
      android:versionName="1.0">
      <application>
           <uses-library android:name="android.test.runner" />
      </application>
      <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="19" />
      <instrumentation android:name="android.test.InstrumentationTestRunner"
       android:targetPackage="com.vendor" />
</manifest>
0(ParentRunner.java:42) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:184) at org.robolectric.RobolectricTestRunner.evaluate(RobolectricTestRunner.java:172) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

The Manifest of my Test Project is like this:

我的测试项目的清单是这样的:

import org.robolectric.annotation.Config;

@Config(emulateSdk = 18) // now @Config(sdk = 18) as of Robolectric 3.0
@RunWith(RobolectricTestRunner.class)
public class SomeTest ...

I complains always about the API Level, no matter what i use.

无论我使用什么,我总是抱怨 API 级别。

Anyone got this working ?

任何人得到这个工作?

回答by Saad Farooq

Update:The annotation is now @Config(sdk = 18)(or @Config(sdk = Build.VERSION_CODES.JELLY_BEAN_MR2)) and the properties file mentioned in link is now robolectric.properties.

更新:注释是 now @Config(sdk = 18)(或@Config(sdk = Build.VERSION_CODES.JELLY_BEAN_MR2)),链接中提到的属性文件是 now robolectric.properties

Original Answer:You can use the @Configannotation to have Robolectric emulate an SDK version. You can put this :

原始答案:您可以使用@Config注释让 Robolectric 模拟 SDK 版本。你可以把这个:

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

This is also possible using a file as mentioned here

这也可以使用这里提到的文件

Not sure what it means for your KitKat specific tests but at least the others should work.

不确定这对您的 KitKat 特定测试意味着什么,但至少其他测试应该有效。

回答by Nicks

In case people like me, still visiting the link for the similar error,

如果像我这样的人仍然访问类似错误的链接,

@Config(emulateSdk = )is not working now. Its changed to sdk--
@Config(constants = BuildConfig.class, sdk=21)

@Config(emulateSdk = )现在不工作。它更改为sdk--
@Config(constants = BuildConfig.class, sdk=21)

For me, I was getting error with target version 22,

对我来说,目标版本 22 出现错误,

SUPPORTED_APIS.put(Build.VERSION_CODES.JELLY_BEAN, new SdkVersion("4.1.2_r1", "0"));
SUPPORTED_APIS.put(Build.VERSION_CODES.JELLY_BEAN_MR1, new SdkVersion("4.2.2_r1.2", "0"));
SUPPORTED_APIS.put(Build.VERSION_CODES.JELLY_BEAN_MR2, new SdkVersion("4.3_r2", "0"));
SUPPORTED_APIS.put(Build.VERSION_CODES.KITKAT, new SdkVersion("4.4_r1", "0"));
SUPPORTED_APIS.put(Build.VERSION_CODES.LOLLIPOP, new SdkVersion("5.0.0_r2", "0"));

and so I emulated it to 21.

所以我将其模拟为 21。

回答by James Muranga

According to SdkConfig.java, Roboelectric only supports the following versions / API levels:

根据SdkConfig.java,Roboelectric 仅支持以下版本/API 级别:

##代码##

Are you sure you have tried those?

你确定你试过这些吗?