73

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 ?

silwar
  • 6,470
  • 3
  • 46
  • 66
Kitesurfer
  • 3,438
  • 2
  • 29
  • 47

3 Answers3

88

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.

Saad Farooq
  • 13,172
  • 10
  • 68
  • 94
  • 1
    It works for me it, is not critical right now to be on API 18. Thanks! – Kitesurfer Jan 20 '14 at 09:37
  • thanks! is there a way to set that globally? I do not want to set ( and later hopefully unset ) this at each test – ligi May 21 '14 at 18:10
  • Yep. According to the link I put in there, an `org.robolectric.Config.properties` file should do that for your entire project. – Saad Farooq May 21 '14 at 18:30
  • Is there any way to do this on a test level instead of a class level? I have behavior in a class that changes based on the API level and would like to test it. – karl Jul 02 '14 at 19:48
  • Good point... maybe you can create a new question or create an issue on Robolectric – Saad Farooq Jul 02 '14 at 19:49
  • Try reading this article and see if it is what you're looking for (if it's still a question after this much time). It helped me in a situation I was experiencing. http://nenick-android.blogspot.de/2015/02/android-studio-110-beta-4-and.html – TheIcemanCometh Feb 25 '15 at 12:12
  • Thanks for the tip, this is mine `@RunWith(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class, sdk = 16)` – Lunf Jul 22 '15 at 07:36
  • 3
    `@Config(emulateSdk = 18)` will no longer work. It needs to be `@Config(sdk = Build.VERSION_CODES.JELLY_BEAN_MR2)`, etc – Tash Pemhiwa Apr 26 '16 at 08:56
28

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.

Nicks
  • 16,030
  • 8
  • 58
  • 65
  • 1
    Thanks! How did you discover this please? I haven't been able to find it documented anywhere. I'm having other similar problems upgrading from 3.0-rc2 to 3.0, and I feel surely they must be documented somewhere...! – Mark Smith Sep 01 '15 at 09:23
  • even I was banging my head.Here is a quick wiki refernce, may be useful to you :-) https://github.com/robolectric/robolectric/wiki/2.4-to-3.0-Upgrade-Guide – Nicks Sep 01 '15 at 09:33
10

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?

StefMa
  • 3,344
  • 4
  • 27
  • 48
James Muranga
  • 808
  • 9
  • 13