has anyone a working example of using @Autowired t...
# spring
h
has anyone a working example of using @Autowired to inject MockMvc in order to test a RestController? Using JUnit 5.3.0, SpringBoot 2.1.2 and kotlin 1.3?
I’m getting “lateinit property mockMvc has not been initialized” on every combo of annotations I have tried…
Seeing as WebTestClient is not working with the versions above - is this also the case for MockMvc?
if that’s correct: any suggestions for testing JSON responses from RestControllers in Kotlin+SpringBoot+JUnit5?
m
I`m using it pretty often not sure that understand you issue How do u configure your tests?
Copy code
@SpringBootTest
@AutoConfigureMockMvc
@ExtendWith(SpringExtension::class)
class Test(
    @Autowired val mockMvc: MockMvc
) { ... }
and of course it depends how your app configured and class-path etc etc Im using SB 2.1.1
t
I have a set up like this
Copy code
lateinit var mvc: MockMvc

  @Inject
  private var context: WebApplicationContext? = null

  @Before
  fun setUpMvc() {
    this.mvc = webAppContextSetup(this.context!!).build()
  }
l
JUnit 5 uses @BeforeEach not @Before I think 🤔
h
using JUnit 5 @mp? I can get it to work with JUnit 4, but with 5 it has trouble initializing the MockMvc 😕
m
yes junit 5
👍 1