I'm experiencing very weird situation AFAIK.
I'm writing an test for Spring Batch Processor which is StepScope using mockito-kotlin. Below is an example.
// Processor to test
@StepScope
@Component
class MyProcessor(
private val myService: MyService
) {
fun process(item: Item) {
// doSomething
myService.foo(arg1, arg2)
}
}
// test
@SpringBootTest
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
class MyProcessorTest(
private val myProcessor: MyProcessor,
@MockBean private val myService: MyService
) {
@Test
fun testA() {
given { myService.foo(any(), any()) } willThrow { IllegalArgumentException("myException") }
myProcessor.process(Item(...))
}
@Test
fun testB() {
given { myService.foo(any(), any()) } willReturn { "SomeValue" }
myProcessor.process(Item(...))
}
}
I ran the above code, and testA was followed by testB, and then testB failed with IllegalArgumentException("myException") even I expected myService.foo() will return "SomeVale" normally
Here is what I found:
- stacktrace was referencing the line of
givenclause. - every test-cases has same instance for
myServicewhich is mock-class
I've created another test class to reproduce this situation, but failed. Mocking normal component(not step-scoped) behaved as I expected. Why is this happening ? Is this just a bug ? or expected case ?
edit)
Below is my own SpringBootTest(without Spring Batch) to debug.
@Component
class MyService {
fun foo(arg1: String, arg2: String) {
// doSomething
}
}
// test
@SpringBootTest
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
class MyProcessorTest(
@MockBean private val myService: MyService
) {
@Test
fun testA() {
given { myService.foo(any(), any()) } willThrow { IllegalArgumentException("myException") }
myService.foo(arg1, arg2) // throw exception as expected
}
@Test
fun testB() {
given { myService.foo(any(), any()) } willReturn { "SomeValue" }
myService.foo(arg1, arg2) // return value as expected
}
}
Similar test-case, but it works well as I expected. Even confused, I changed my stubbing method from BDD-style stubbing(given().willReturn()) into doReturn().when(mock).myMethod(), then it works well ! So I thought this should be a bug, right ?