Difference between using MockMvc with SpringBootTest and Using WebMvcTest

后端 未结 3 1521
别跟我提以往
别跟我提以往 2020-12-02 04:34

I am new to Spring Boot and am trying to understand how testing works in SpringBoot. I am a bit confused about what is the difference between the following two code snippets

3条回答
  •  日久生厌
    2020-12-02 05:03

    @SpringBootTest annotation tells Spring Boot to go and look for a main configuration class (one with @SpringBootApplication for instance), and use that to start a Spring application context. SpringBootTest loads complete application and injects all the beans which can be slow.

    @WebMvcTest - for testing the controller layer and you need to provide remaining dependencies required using Mock Objects.

    Few more annotations below for your reference.

    Testing slices of the application Sometimes you would like to test a simple “slice” of the application instead of auto-configuring the whole application. Spring Boot 1.4 introduces 4 new test annotations:

    @WebMvcTest - for testing the controller layer
    @JsonTest - for testing the JSON marshalling and unmarshalling
    @DataJpaTest - for testing the repository layer
    @RestClientTests - for testing REST clients
    

    Refer for more information : https://spring.io/guides/gs/testing-web/

提交回复
热议问题