Hi! What do you usually use for unit tests and moc...
# server
j
Hi! What do you usually use for unit tests and mocking in spring boot projects? I've been trying to set up springmockk with spring boot 2.3.5 and kotlin 1.4.2 but haven't been able to set it up properly
m
i use junit jupiter and mockk, but i may be biased about mockk (i’m a maintainer of the project)
what issues are you having with springmockk? i’m not directly involved in the project but i may help
🙏 1
t
I'm using springmockk (3.0.0) and just upgraded to Spring Boot 2.4.0 and Kotlin 1.4.20. Works fine for me.
j
I have a simple test class, I added the gradle dependency like explained here https://github.com/Ninja-Squad/springmockk . The test aims to use a controller, which depends on two services, so I set it up like so:
Copy code
@ExtendWith(SpringExtension::class)
@WebMvcTest
class LastValuesControllerTest {

    @MockkBean
    private lateinit var lastValuesService: LastValuesService

    @MockkBean
    private lateinit var authService: AuthorizationService

    @Autowired
    private lateinit var controller: LastValuesController

    @Test
    fun `simple test`() {
        assert(true)
    }
}
When I build the project I get the following error:
Copy code
LastValuesControllerTest > initializationError FAILED
    java.lang.NoClassDefFoundError at MockkContextCustomizerFactory.kt:28
        Caused by: java.lang.ClassNotFoundException at BuiltinClassLoader.java:581
        Caused by: java.lang.ClassNotFoundException at BuiltinClassLoader.java:581
Spring Boot: 2.3.5.RELEASE Kotlin: 2.4.2 SpringMockk: 3.0.0
Turns out, I didn't read the docs fully, my bad 😅 As per the springmockk README: • Version 3.x of SpringMockK: compatible with Spring Boot 2.4.x • Version 2.x of SpringMockK: compatible with Spring Boot 2.2.x and 2.3.x • Version 1.x of SpringMockK: compatible with Spring Boot 2.1.x Leaving this here in case someone runs into the same problem
m
ha, there it is, thanks for spotting it!