Hesam
Hesam

Reputation: 53600

Mocked method doesn't return expected value

I want to test a method of my class using Mockito.

public class SplashPresenter {
    public volatile State mField1 = State.DEFAULT;
    public volatile State mField2 = State.DEFAULT;

    boolean stateFlagsAreAllCompleted(@NonNull final ISplashView view) {
        if (mField1 == State.COMPLETE //
                && mField2 == State.COMPLETE) {

            // Check Forced Update
            final int updateCheckResult = checkForcedUpdate(); // <===
            if (updateCheckResult == MyConstants.PRODUCTION_UPDATE_AVAILABLE) {
                view.displayForcedUpdateAlert(false);
                return true;
            }

            if (updateCheckResult == MyConstants.BETA_UPDATE_AVAILABLE) {
                view.displayForcedUpdateAlert(true);
                return true;
            }

            view.restartLoader();
            // Move to the home screen
            return true;
        }

        return false;
    }

    int checkForcedUpdate() {
    ...... // my codes
    }
}

and this is my test class:

public class SplashPresenterTest_ForStateFlags {

    private Context mContext;
    private ISplashView mView;

    @Before
    public void setUp() throws Exception {
        mContext = Mockito.mock(Context.class);
        mView = Mockito.mock(ISplashView.class);
    }

    @Test
    public void stateFlagsAreAllCompleted() throws Exception {
        SplashPresenter presenter = Mockito.mock(SplashPresenter.class);
        presenter.mField1 = State.COMPLETE;
        presenter.mField2 = State.COMPLETE;

        when(presenter.checkForcedUpdate()).thenReturn(1);

        boolean actual = presenter.stateFlagsAreAllCompleted(mView);
        System.out.println("actual: " + actual + ", " +
                presenter.mField1 + ", " +
                presenter.checkForcedUpdate());
        assertTrue(actual);
    }
}

Test failure is what happens at the end. This is the output:

actual: false, COMPLETE, 1

The thing that I don't understand is even I change the stateFlagsAreAllCompleted method to following code then still test fails with above output.

boolean stateFlagsAreAllCompleted(@NonNull final ISplashView view) {

    return true;
}

Upvotes: 0

Views: 550

Answers (1)

ucsunil
ucsunil

Reputation: 7494

You've not yet mocked the behavior for the method stateFlagsAreAllComplete. You need to do:

when(presenter.stateFlagsAreAllComplete(Matchers.any()).thenReturn(true);

You can fine tune the Matchers.any() argument to the class type you want.

EDIT: I see that you are trying the test the method stateFlagsAreAllComplete. Since you are trying to test the method stateFlagsAreAllComplete of the class SplashPresenter, you cannot do so by mocking the class whose method is under test. You will have to work with an instance of the class. Mocked methods should only be used while testing when they are called in another method under test.

You will have to create an instance of the class you want to test.

Upvotes: 1

Related Questions