-1

The method will be called two times with different parameters:

  1. method("test1")
  2. method("test2")

In my test I am trying to check if the parameters are right or not

`when`(
    mockClass.method(
        anyObject()
    )
).then { param ->
    val value: Class = param.arguments[0] as Class
    assert(value == "test1")
}


`when`(
    mockClass.method(
        anyObject()
    )
).then { param ->
    val value: Class = param.arguments[0] as Class
    assert(value == "test2")
}

I understand why it is not working but is there a proper way to verify the same method two times?

I am trying to verify the same method which will be called two times with different parameters and want to check if are given parameters are right?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Naomi
  • 1
  • 3

1 Answers1

1

First of all when you want to assert that something is called with some parameter then use verify not when->assert. And you can write multiple verify's in a row like.

verify(mockClass).method("test1")
verify(mockClass).method("test2")

You can also use inOrder to verify correct order. More is described here for example: Mockito verify order / sequence of method calls

Tarmo
  • 3,851
  • 2
  • 24
  • 41