I have an integration test:
class Test(
private val classA: ClassA
) {
@Inject
lateinit var classB: ClassB
@Test
fun testSomething(){
classA.someMethod()
verify(classB, times(1)).anotherMethod(arg1, arg2)
}
@MockBean(ClassB::class)
fun classB(): ClassB {
return mock<ClassB>()
}
}
class ClassA(
private val classB: classB
){
...
classB.anotherMethod(someArg, someArg)
...
}
and I would like to verify that the method of classB is invoked with the correct two parameters. However the verification of the invocation fails every time as it would appear that the method invoked in the test is not part of the mock, but rather of its own instance.
I have tried by instantiating classA with the mock of classB, but that did not work. I have also tried injecting classB instead of passing it as a constructor parameter, but that didnt seem to do the trick either, as I still get "Wanted but not invoked:". Is there a way to verify method invocation of actual instances?
In your code you are first instanciating ClassA before initializing the @MockBean ClassB.
Also in your question you are stating that you have attempted to set ClassB directly to ClassA. This is not visible from the code and even more with this particular code the only way this to happen is if you have a @Bean of the mock created externaly that is passed to classA and at the same time injected in the Test. It is not visible from the code that your Test is actualy a spring managed bean.