Android: Robolectric does not support API level 1

2019-02-02 12:04发布

问题:

This is my basic test class:

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

When I execute my test under Android Studio, with the Terminal window, I have this error:

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)

Gradle informations:

compileSdkVersion 19
buildToolsVersion "19.0.1"

    minSdkVersion 14
    targetSdkVersion 19
    versionCode 2

androidTestCompile 'org.robolectric:robolectric:2.+'
androidTestCompile 'junit:junit:4.+'

I use the last Android Studio version: 0.5.8

Thank you guys!

回答1:

There are several approaches for this:

  1. Specify in your AndroidManifest.xml your targetSdkVersion
  2. Create a custom RobolectricTestRunner and override getAppManifest method for specifying the target sdk. E.g.:

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

    and then use it in your MainActivityTest class with @RunWith(MyCustomRunner.class).

  3. Use @Config(emulateSdk=18) annotation on top of your MainActivityTest class.

The third approach is the simplest one, but you have to annotate all of your test classes. I personally prefer the second approach as gives more flexibility in configuring your runner.



回答2:

Based on Andrei answer I tried to solve my problem, but actually I think that @Config(emulateSdk=18) has been removed, and this is what solved my issue:

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


回答3:

Notes for Mac users

If you are on a Mac, you will probably need to configure the default JUnit test runner configuration in order to work around a bug where IntelliJ / Android Studio does not set the working directory to the module being tested. This can be accomplished by editing the run configurations, Defaults -> JUnit and changing the working directory value to $MODULE_DIR$

Setup:

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

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