Difference between using MockMvc with SpringBootTest and Using WebMvcTest

后端 未结 3 1523
别跟我提以往
别跟我提以往 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:07

    @SpringBootTest is the general test annotation. If you're looking for something that does the same thing prior to 1.4, that's the one you should use. It does not use slicing at all which means it'll start your full application context and not customize component scanning at all.

    @WebMvcTest is only going to scan the controller you've defined and the MVC infrastructure. That's it. So if your controller has some dependency to other beans from your service layer, the test won't start until you either load that config yourself or provide a mock for it. This is much faster as we only load a tiny portion of your app. This annotation uses slicing.

    Reading the doc should probably help you as well.

提交回复
热议问题