I'm using @Nested in JUnit5 to thematically group tests. Following published examples and a pattern I've used in both Java and Kotlin previously. However the Spring annotations associated with the outer test class are not applying to the inner classes as I'd expect. Is this a bug or my bad assumptions?
I'm testing a RESTful endpoint in Spring with the @SpringBootTest and @AutoConfigureMocMvc annotations. I wanted to group them into things like authentication, get, post, put etc. I grouped them using @Nested and inner classes as per past experience and examples. However, I found that unless I copied all the annotations from the outer test class to every inner class declaration nothing worked. I don't believe this was the case in Java and isn't implied in the examples.
Fails:
@SpringBootTest
@AutoConfigureMockMvc
class EndpointTest {
// ... @Autowire's, mocks etc...
@Nested
inner class AuthTest {
@Test
fun `should fail with bad auth`() {
// some logic
}
}
}
Succeeds:
@SpringBootTest
@AutoConfigureMockMvc
class EndpointTest {
// ... @Autowire's, mocks etc...
@SpringBootTest
@AutoConfigureMockMvc
@Nested
inner class AuthTest {
@Test
fun `should fail with bad auth`() {
// some logic
}
}
}
I would have assumed, and the examples would seem to support, that the inner class should not need to have all the annotations copied. I don't think in Java it does. Is this something about Springs annotation code with regards to Kotlin?