0

I have an Android application that I test using @RunWith(RobolectricTestRunner.class). When I run all tests together in Android Studio, it works. But when I run a test or test class separately, I get this error:

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

Note that both the minimum and target API levels are specified in the manifest file. It seems that the environment is not setup properly. Adding @Config(emulateSdk=23) makes the test start but the test fails while accessing resources.

Any idea of what could cause the test environment not to load properly?

JM Lord
  • 1,031
  • 1
  • 13
  • 29

1 Answers1

1

Check your gradle and your class :

Gradle :

 testCompile 'org.robolectric:robolectric:3.3'
 testCompile 'junit:junit:4.12'
 testCompile 'org.assertj:assertj-core:1.7.1'

In your test Class :

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 25)
public class YourTest {

    @Test
    public void shouldNotBeNull() throws Exception {
       //put your test here for example
    }
)

YourTest class should be put in Test folder (not AndroidTest folder)

On Android Studio :

  • Edit Configurations
  • In Junit, you have to change the working directory to $MODULE_DIR$. The important thing is $MODULE_DIR$.
Dany Pop
  • 3,590
  • 2
  • 21
  • 28
  • Changing the directory manually to $MODULE_DIR$ did the trick, thanks! Wonder however why I should have to do this manually for every test configuration, can't it be inferred by the IDE? – JM Lord Jun 16 '17 at 18:03
  • Note, as of Android Studio 4.1.2, $MODULE_DIR$ has been replaced with $ContentRoot$ – Entreco Jan 28 '21 at 16:09