0

Here is my test method where It should be success if showLoading() and loadDataSuccess(response) was called:

 @RunWith(PowerMockRunner.class)
     public class PresenterTest {
            @Mock  
            private ProfileContract.View view;
            @Mock
            private ProfileContract.Handler handler;

            @Test
            public void onLoadDataClicked() {
              presenter.loadData();
              verify(mView, times(1)).showLoading();
              verify(mHandler, times(1)).loadDataSuccess();
            }
     }

UPDATE 1 Here is my presenter:

class ProfilePresenter(private val mView: ProfileContract.View) : ProfileContract.Handler {

     override fun loadData() {
            mView.showLoading()
            mUserService.user()
                    .compose(RxUtil.mapper())
                    .subscribe({ response ->
                        loadDataSuccess()
                    }, { error ->
                        //stuff
                    })
        }
}

Thanks!

azsoftco
  • 812
  • 1
  • 8
  • 20

2 Answers2

0

If you use return statment, your test finish with success status.

Mamykin Andrey
  • 1,352
  • 10
  • 13
0

I think there is a basic problem with your test setup:

You do not use verify to check if one function calles another function within the same class. Verify is used to verify that the tested class calls function on other (mocked) classes. If I am not mistaken, your setup should actually give you an error message saying that you can not use verify on instantiated classes.

What you should do -if you want to check if onCompleteClicked() produces the correct results- is to check if the data that gets changed inside the onStuffComplete() function is set correctly. You can use an assert for that.

As an example, lets say onStuffCompleted() sets completeCounter to 1

@Test
public void onCompleteClicked() {
  presenter.onStuffCompleteClicked();
  assertEquals(completCounter , 1);
}

And to answer your original question: verify (and assert) will pass if the requirements were met (and by this the whole test will pass) and fail if not. You do not need to add any additional stuff (but once again: verify will only work with mocked classes).

Bmuig
  • 1,059
  • 7
  • 12