Question

I'm using Robolectric 2.2 and want to improve my tests a bit. Currently I want to check if a method of mine calls android.support.v4.app.FragmentManager.beginTransaction(). The source to test contains lines like:

  public void setFragmentManager(FragmentManager fragmentManager) {
    this.fragmentManager = fragmentManager;
  }

  public void onTabChanged(String tabId) {
    :
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

This should make it easy to mock such a FragmentTransaction. I guess I know how to do this with Mockito, but Robolectric claims that no further mocking framework is required:

No Mocking Frameworks Required

An alternate approach to Robolectric is to use mock frameworks such as Mockito or Android Mock to mock out the Android SDK. While this is a valid approach, it often yields tests that are essentially reverse implementations of the application code.

Robolectric allows a test style that is closer to black box testing, making the tests more effective for refactoring and allowing the tests to focus on the behavior of the application instead of the implementation of Android. You can still use a mocking framework along with Robolectric if you like.

Now I'm not sure if I can't find how to solve my problem with Robolectric or I should go ahead and use Mockito. I tried to make it work with org.fest.reflect.* stuff, but I failed.

What I'd like to know is:

  1. How do I do this with Robolectric?
  2. Or should I use Mockito?
Was it helpful?

Solution

I've just quickly checked Robolectric shadow classes and FragmentManager API. And I haven't any method that helps with pending transactions.

So I would suggest you to use Mockito until there are such methods. For sure Robolectric usage is preferred over Mockito because of better code design (no unnecessary injections like in your case FragmentManager)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top