Logging Mockito invocations in Spring

@ExtendWith(SpringExtension::class)
abstract class AbstractSpringUnitTest {

    private val logger = LoggerFactory.getLogger(AbstractSpringUnitTest::class.java)

    @Autowired
    protected lateinit var ctx: ApplicationContext

    @AfterEach
    fun logMockingDetails() {
        ctx.beanDefinitionNames.forEach {
            val bean = ctx.getBean(it)
            if (MockUtil.isMock(bean)) {
                Mockito.mockingDetails(bean)
                    .invocations.forEach { invocation -> logger.info("Mock invocation: $invocation") }
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *