Anton Kizema
Anton Kizema

Reputation: 1092

setup and write demo test the robolectric in Android Studio

Do anyone know how to integrate robolectric into android studio? How to write sample test? How to launch it?

I am working with android studio not to long, and I am too bad with gradle. Searching the net didn't give me a result - I even could not launch official demo - https://github.com/robolectric/robolectric-samples . My android studio do not saw the test class.

Please give me simpliest step by step gide, thanks

Upvotes: 1

Views: 188

Answers (1)

Gregor Zeitlinger
Gregor Zeitlinger

Reputation: 439

Since robolectric runs in a JVM (i.e. not on a device or emulator), it is just a library and adding the test runner is all that's needed.

Make sure that the android SDK is later in the classpath than robolectric or junit - otherwise you'll get the stubbed methods from the android SDK.

@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {

@Test
public void shouldHaveApplicationName() throws Exception {
    String appName = new MyActivity().getResources().getString(R.string.app_name);
    assertThat(appName, equalTo("MyActivity"));
}

}

See http://robolectric.org/quick-start/

Upvotes: 1

Related Questions