0

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?

nwillc
  • 103
  • 1
  • 6
  • Possible duplicate of [Transaction roll back is not working in test case in @Nested class of JUnit5](https://stackoverflow.com/questions/44203244/transaction-roll-back-is-not-working-in-test-case-in-nested-class-of-junit5) – Sam Brannen Oct 02 '19 at 22:21

1 Answers1

1

That's to be expected since "pseudo-inheritance" of annotations from enclosing classes is not yet supported in the Spring Framework (neither for Java nor for Kotlin).

See the following answer for details: https://stackoverflow.com/a/44227179/388980

Sam Brannen
  • 29,611
  • 5
  • 104
  • 136