I'm pretty new to testing so I might be doing something wrong.I'm trying to capture the values that are passed to a method
@Mock
private lateinit var service: TestService
@InjectMocks
private lateinit var underTest: UnderTestService
@org.junit.jupiter.api.Test
fun `testMethod`() {
//given
val var1 = Test.Value
val var2 = TestClass::class.java
val var3 = listOf(Entry1(), Entry2())
//when
underTest.method(var1, var2, var3)
val argumentCaptor = ArgumentCaptor.forClass(String::class.java)
verify(service, times(2)).method(
argumentCaptor.capture(),
argumentCaptor.capture()
)
Here, after doing verify the argumentCaptor.capture() return null for some reason and I don't understand what am I doing wrong?
java.lang.NullPointerException: argumentCaptor.capture() must not be null
I think that it is kotlin related, the signature of the method that I'm trying to get the parameters looks like this
fun method(param1: String, vararg param2: String?) {
//do something
}
CodePudding user response:
MockitoKotlinHelpers.kt can help you here. The capture function provides a way to call ArgumentCaptor.capture()
Example:
verify(service, times(2)).method(
capture(argumentCaptor),
capture(argumentCaptor));