lundi 31 décembre 2018

Mockito - a test makes another one fail

If I run each test individually, they will pass. If I run them together, only the first one passes (onGetAmountSuccess). The other ones show the error Message: Wanted but not invoked: view.showErrorMessage();

I have already tried to use Mockito.reset but it did not work.

 class Test {

private  var myRepository: MyRepository = mock()
private  var view : MyContract.View = mock()
private  var presenter : Presenter =  MyPresenter(myRepository)

@Before
fun setup() {
    RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }
    presenter.attach(view)
}


@Test
fun onGetAmountSuccess(){
    doAnswer { Single.just(Currency("EUR", "12/31/2018", arrayListOf()))}.whenever(myRepository).getRates()
    presenter.getRates()
    verify(view, times(1)).updateAmount(arrayListOf)
}

@Test
fun onGetAmountFailure(){
    doAnswer { Single.error<Currency>(Exception("Error"))}.whenever(myRepository).getRates()
    presenter.getRates()
    verify(view, times(1)).showErrorMessage()
}

@Test
fun onUpdateAmountFailure(){
    doAnswer { Single.error<Currency>(Exception("Error"))}.whenever(myRepository).updateAmount("USD", 100.toBigDecimal())
    presenter.updateAmount("USD", 100.toBigDecimal())
    verify(view, times(1)).showErrorMessage()

}

}

Presenter:

 override fun updateAmount(base : String, amount: BigDecimal) {
    myRepository.
        updateAmount(base,  amount)
        .compose(applySingleSchedulers())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe (
            {currency ->
                view?.updateAmount(amount)

            },
            {
                view?.showErrorMessage()
            }
        ).apply {
            disposables.add(this)
        }
}


   override fun getRates() {
    myRepository.getRates()
        .compose(applySingleSchedulers())
        .observeOn(AndroidSchedulers.mainThread())
         .repeatWhen{ done -> done.delay(1, TimeUnit.SECONDS) }
        .subscribe (
            {currency ->
                view?.updateAmount(amount)

            },
    {
        view?.showErrorMessage()
    }
    ).apply {
            disposables.add(this)
        }
}

I have also tried to use mockito reset, but it did not work:

@After
fun reset(){
    Mockito.reset(view)
    Mockito.reset(myRepository)
    presenter = MyPresenter(myRepository)
    presenter.attach(view)

}

I am using the following lib:

com.nhaarman.mockitokotlin2:mockito-kotlin:2.0.0

Does any one know what I am doing wrong and how to fix it? Thanks!!

Aucun commentaire:

Enregistrer un commentaire