JM Lord
JM Lord

Reputation: 1197

Robolectric fails to load properly when launching single test

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?

Upvotes: 0

Views: 578

Answers (1)

Dany Pop
Dany Pop

Reputation: 3648

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

Upvotes: 1

Related Questions