dierre
dierre

Reputation: 7210

Is it possible to do integration tests with Robolectric?

I have MainActivity and SubActivity.

MainActivity has a button triggering a startActivityForResult calling SubActivity.

The SubActivy has the responsibility to add a record to a certain repository so that when it calls finish, the MainActivty, in the method onActivityResult, has to call the notifyDataSetChanged on the adapter:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if(resultCode == RESULT_OK && requestCode == 1) {
                    adapter.notifyDataSetChanged();
            }
    }

Now, is there a way to test this come and go with Robolectric? Right now I'm able to test the intent called with a click from MainActivity to SubActivity (using Shadow objects), but I can't see no way to trigger the finish on SubActivity (with the new element added to the repository) so I can check that the adapter is showing the new element on MainActivity

I'm new to Roboelectric so I don't if what I want to test is beyond what this framework is about. Should I use Mockito?

Upvotes: 3

Views: 1387

Answers (2)

Erich Douglass
Erich Douglass

Reputation: 52002

Robolectric is not an integration test framework. It is a framework that allows you to write unit tests and run them on a desktop JVM. Eugen's answer is spot on: Test each activity in isolation with Robolectric. If you want to test an entire flow through your application (which spans multiple activities, services, etc) use Google's InstrumentationTestCase framework, Robotium, or Calabash.

Upvotes: 3

Eugen Martynov
Eugen Martynov

Reputation: 20130

I would have two unit tests for both activities.

The MainActivityTest:

  1. Check that SubActivity intent started
  2. Check that onActivityResult refreshes the list on RESULt_OK

The SubActivityTest:

  1. Adds record to database
  2. Finishes with RESULT_OK

For the entire acceptance tests I would use Robotium or Calabash

Upvotes: 4

Related Questions