0

I use mockk (versions 1.12.0 and 1.12.3) in @SpringBootTest in order to create a spy of some service and verify later that the service's method was (or was not) called. This looks like this:

@SpringBootTest
class MyTests {
    @Autowired
    private lateinit var someBean: SomeBean

    @TestConfiguration
    class Config {
        @Bean
        @Primary
        fun someBeanSpy(someBean: SomeBean) = spyk(someBean)
    }

    @BeforeEach
    internal fun setUp() {
        clearAllMocks()
    }

    @ParameterizedTest
    @MethodSource("data")
    fun `some test`(s: String) {
        // prepare data and call controller
        verify(exactly = 0) { someBean.foo(any(), any(), any()) } // <- execution of this verify lasts about 6 seconds
        // other verify statements last about 200ms
    }

    companion object {
        @JvmStatic
        fun data(): Stream<Arguments> = Stream.of(Arguments.of("abc"), Arguments.of("cde"))
    }
}

However, the execution of verify method on spy's methods with 3 or more parameters take too long time. Why may I have such behavior?

The same works fine with Mockito, but I don't want to use it with Kotlin because than I can't use just regular Mockito#any with non-nullable types.

Also, when I reduced the SomeBean#foo method's number of parameters from 3 to 2, verify executed just as normal, about 200ms.

cutiko
  • 9,887
  • 3
  • 45
  • 59
Kalich
  • 87
  • 2
  • 6
  • First verify block may take time to construct Mockk internal objects. If your bean is never used, it might be faster to use `verify(exactly = 0) { someBean wasNot Called }`. – ocos Aug 09 '22 at 13:18
  • 1
    Suggestion: IntegrationTests are not made to verify behavior. They are used to verify configuration and dependency connections. You should slice parts down to a UnitTest and use mocks there. IntTest should just cover things like write/read from DB and assert result but not verify what was called. Just result matters. – LenglBoy Aug 09 '22 at 14:29
  • @ocos I though about first block too, but it takes long time even if it is not a first block, e.g. third or second, but previous blocks run okay. I also tried `wasNot Called`, but it seems it doesn't work for spies. – Kalich Aug 09 '22 at 21:08
  • @LenglBoy I agree with you, but for now I am at least just interested, what is the cause of the `verify` being executed so long. – Kalich Aug 09 '22 at 21:10
  • `wasNot Called` works also for spies. We don't have full code here but if you use any method of `someBean`, `wasNot Called` verification will be failed. That bean should be Jack's Beanstalk. – ocos Aug 09 '22 at 22:52

0 Answers0