Does Robolectric support API level?

AndroidRobolectric

Android Problem Overview


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:

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.access$100(RobolectricTestRunner.java:57)
at org.robolectric.RobolectricTestRunner$2.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$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.robolectric.RobolectricTestRunner$1.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:

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

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

Anyone got this working ?

Android Solutions


Solution 1 - Android

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.

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

import org.robolectric.annotation.Config;

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

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.

Solution 2 - Android

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)

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

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

and so I emulated it to 21.

Solution 3 - Android

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

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"));

Are you sure you have tried those?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKitesurferView Question on Stackoverflow
Solution 1 - AndroidSaad FarooqView Answer on Stackoverflow
Solution 2 - AndroidNicksView Answer on Stackoverflow
Solution 3 - AndroidJames MurangaView Answer on Stackoverflow