Android: Robolectric does not support API level 1

♀尐吖头ヾ 提交于 2019-11-30 03:43:58

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.

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 {
  ......
  }

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!