https://kotlinlang.org logo
Title
j

João Rodrigues

12/02/2020, 1:29 PM
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

Mattia Tommasone

12/02/2020, 1:32 PM
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
:thank-you: 1
t

Tobias Berger

12/02/2020, 2:08 PM
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

João Rodrigues

12/02/2020, 2:32 PM
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:
@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:
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

Mattia Tommasone

12/16/2020, 9:02 PM
ha, there it is, thanks for spotting it!