Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing that button starts an Activity with Robolectric

Hi I have the following code:

@RunWith(Test9Runner.class) public class MainActivityTest  {     private MainActivity activity;     private Button pressMeButton;      @Before     public void setUp() throws Exception      {         activity = new MainActivity();         activity.onCreate(null);         pressMeButton = (Button) activity.findViewById(R.id.button1);     }      @Test     public void shouldUpdateResultsWhenButtonIsClicked() throws Exception      {         pressMeButton.performClick();         ShadowActivity shadowActivity = shadowOf(activity);         Intent intent = shadowActivity.getResultIntent();         System.out.print(intent.toString());     } } 

But I have no idea how to test that pressing pressMeButton started a new Activity. Actually it does, but how to write the correct Robolectric unit test for this fact?

like image 553
user739684 Avatar asked May 05 '11 10:05

user739684


People also ask

How Robolectric works?

Robolectric works by creating a runtime environment that includes the real Android framework code. This means when your tests or code under test calls into the Android framework you get a more realistic experience as for the most part the same code is executed as would be on a real device.

What is the significance of Robolectric?

Robolectric provides a JVM compile version of the android. jar file. Robolectric handles views, resource loading, and many other things that are implemented in the Android native. This enables you to run your Android tests in your development environment, without requiring any other setup to run the test.

Is your Robolectric test running on the JVM or on an Android emulator What is the advantage of this?

The major benefit of Robolectric is speed. Unit tests with Robolectric do not require a running emulator or device to run the tests and are thus much much faster.


1 Answers

In Robolectric 2.1.1 you can verify if Intent starting new Activity was emitted in following way.

@RunWith(RobolectricTestRunner.class) public class MyTest {   private ShadowActivity shadowActivity;   private MyActivity activity;    @Before   public void setup() {     activity = new MyActivity();     shadowActivity = Robolectric.shadowOf(activity);           }    @Test   public shouldStartNewActivityWhenSomething() {     //Perform activity startup     //Do some action which starts second activity, for example View::performClick()     //...     //Check Intent     Intent intent = shadowActivity.peekNextStartedActivityForResult().intent;     assertThat(intent.getStringExtra(MySecondActivity.EXTRA_MESSAGE)).isEqualTo("blebleble");     assertThat(intent.getComponent()).isEqualTo(new ComponentName(activity, MySecondActivity.class));   } } 

This is similar to what I am doing. Please note that creating Activity by calling new Activity() will make Robolectric print warnings about creating activity improperly, this probably can be done better...

like image 154
MichK Avatar answered Sep 17 '22 13:09

MichK