I have an Activity
public class MyActivity extends ActionBarActivity { public int i; @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); i = getSomeInt(); } protected int getSomeInt() { return 1;
and I want to test it with robolectric 3.0 and mockito .
But I need to make fun of getSomeInt() methiod.
@RunWith(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class) public class MyActivityTest { private MyActivity mActivity; @Before public void setUp() { mActivity = spy(Robolectric.buildActivity(MyActivity.class).create().get()); doReturn(2).when(mActivity).getSomeInt();
Is it possible to use an already mocked method by creating activity?
EDIT
I tried
@Before public void setUp() { ActivityController<MyActivity> co = Robolectric.buildActivity(MyActivity.class); mActivity = spy(co.get()); doReturn(2).when(mActivity).getSomeInt(); co.create(); }
but it seems to me that onCreate not called by mActivity
assertEquals(2, mActivity.i); // gives the result:
java.lang.AssertionError: Expected :2 Actual :0
thanks Eugen for attention, it does not work, because create calls a controller that works with activity that is not checked
source share