Hello folks,
I'd like to test some rest controller. I can't use @WebMvcTest cause my security config uses JWT Token Provider which is @Component itself and will be filtered out by @WebMvcTest
I can't use @SpringBootTest either, because it ignores HttpMessageConvertersAutoConfiguration and I get during test only (this means it works in prod)
org.springframework.http.converter.HttpMessageNotWritableException
Can you please add support of HttpMessageConvertersAutoConfiguration for @AutoConfigureMockMvc?
Here is sample code:
@SpringBootTest(classes = {SomeControllerUnderTest.class})
@AutoConfigureMockMvc
@WithMockUser
class SomeControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private SomeMockService service;
@Test
void foo() throws Exception {
... setup mock ...
mockMvc.perform(get("/api/some"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE));
}
}
If I change @SpringBootTest and remove classes it works just fine, but initializes ALL the crap I don't need:
@SpringBootTest
@AutoConfigureMockMvc
@WithMockUser
class SomeControllerTest {
Thanks!
Comment From: frekele
I have similar behavior too.
Comment From: wilkinsona
HTTP message converter auto-configuration will be included if you use @AutoConfigureWebMvc
. I don't think it should be added to the @AutoConfigureMockMvc
as it's not specific to MockMvc-based testing. @WebMvcTest
uses both @AutoConfigureWebMvc
and @AutoConfigureMockMvc
and it looks like you want both as well.
Comment From: tillias
@wilkinsona many thanks, it works!
@frekele if you're interested here is custom annotation you can use: https://stackoverflow.com/questions/64648893/autoconfiguremockmvc-how-to-set-message-converters
Comment From: rehevkor5
Would be nice to add some explanation about this into https://spring.io/guides/gs/testing-web/ which shows use of @SpringBootTest
with @AutoConfigureMockMvc
but doesn't mention anything about it causing HttpMediaTypeNotAcceptableException
or HttpMessageNotWritableException
or why or what to do about it.
Comment From: wilkinsona
@rehevkor5 Could you please make that suggestion in the guide's repo: https://github.com/spring-guides/gs-testing-web/issues?