Morozov
Morozov

Reputation: 5260

Problem with running the first test on the Robolectric

Strange behavior. I'm just setting up the Robolectric now.

Here are my settings:

build.app

dependencies {
    testImplementation 'org.robolectric:robolectric:4.0-alpha-3-SNAPSHOT'
}

and

testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }

build.project

repositories {
     maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}

So that is my activity

@RunWith(RobolectricTestRunner.class)

public class RootActivityTest {
private RootActivity activity;

@Before
public void setup()  {
    activity = Robolectric.buildActivity(RootActivity.class)
            .create()
            .resume()
            .get();
}

@Test
public void checkActivityNotNull() throws Exception {
    assertNotNull(activity);
}
}

And now when I run a test with this configuration, I get the following error:

java.lang.RuntimeException: MultiDex installation failed (/tmp/robolectric-Method_checkActivityNotNull6294215780013113020/com.mandarine.android-sourceDir (Is a directory)).

Tried to implement one of this library:

testImplementation "org.robolectric:multidex:3.4.2"

or

testCompile "org.robolectric:shadows-multidex:3.0"

But doesn't help.

Upvotes: 2

Views: 533

Answers (1)

Morozov
Morozov

Reputation: 5260

Perhaps someone will be useful. Also u can start from this link

My experience how to run tests with Robolectric:

build.grade:

testImplementation 'org.robolectric:robolectric:3.8'

testOptions {
    unitTests {
        includeAndroidResources = true
    }
}

Here is my MainActivity:

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)

public class MainActivityTest {

@Test
public void shouldNotBeNull() throws Exception {
    MainActivity activity = Robolectric.setupActivity(MainActivity.class);
    assertNotNull(activity);
}
}

And last trick, add in gradle.properties:

android.enableAapt2=false

Upvotes: 1

Related Questions