Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of HttpMessageConvertersAutoConfiguration for @AutoConfigureMockMvc #24000

Closed
tillias opened this issue Nov 2, 2020 · 5 comments
Closed
Labels
status: declined A suggestion or change that we don't feel we should currently apply

Comments

@tillias
Copy link

tillias commented Nov 2, 2020

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!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 2, 2020
@frekele
Copy link

frekele commented Nov 2, 2020

I have similar behavior too.

@wilkinsona
Copy link
Member

wilkinsona commented Nov 2, 2020

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.

@wilkinsona wilkinsona added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 2, 2020
@tillias
Copy link
Author

tillias commented Nov 2, 2020

@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

@rehevkor5
Copy link
Contributor

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.

@wilkinsona
Copy link
Member

@rehevkor5 Could you please make that suggestion in the guide's repo: https://github.com/spring-guides/gs-testing-web/issues?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

5 participants