MockMvc在测试Web层时为null。

huangapple go评论96阅读模式
英文:

MockMvc is null when testing the Web Layer

问题

我在我的应用程序中有这个MvcTest:

  1. @SpringBootTest
  2. @WebMvcTest
  3. public class BarsControllerTest {
  4. @Autowired
  5. private MockMvc mockMvc;
  6. @Test
  7. public void testBars() throws Exception {
  8. mockMvc.perform(get("/bars")
  9. .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
  10. .andExpect(status().isOk())
  11. .andExpect(jsonPath("$.length()", is(1)));
  12. }
  13. }

但是当我运行测试时,mockMvc在运行测试时为null。

英文:

I have this MvcTest in my application:

  1. @SpringBootTest
  2. @WebMvcTest
  3. public class BarsControllerTest {
  4. @Autowired
  5. private MockMvc mockMvc;
  6. @Test
  7. public void testBars() throws Exception {
  8. mockMvc.perform(get("/bars")
  9. .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
  10. .andExpect(status().isOk())
  11. .andExpect(jsonPath("$.*", hasSize(1)));
  12. }
  13. }

but when I run the test mockMvc is null when running the tests.

答案1

得分: 1

不应该同时使用 @WebMvcTest@SpringBootTest

如果你想要测试 Web 层和其他层,可以同时使用 @AutoConfigureMockMvc@SpringBootTest

  1. @SpringBootTest
  2. @AutoConfigureMockMvc
  3. public class BarsControllerTest {
  4. @Autowired
  5. private MockMvc mockMvc;
  6. @Test
  7. public void testBars() throws Exception {
  8. mockMvc.perform(get("/bars")
  9. .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
  10. .andExpect(status().isOk())
  11. .andExpect(jsonPath("$.*", hasSize(1)));
  12. }
  13. }

或者,如果你只想测试 Web 层,可以只使用 @WebMvcTest,请注意这不会加载完整的 Spring 应用程序上下文(它只加载 Web 层):

  1. @WebMvcTest
  2. public class BarsControllerTest {
  3. @Autowired
  4. private MockMvc mockMvc;
  5. @Test
  6. public void testBars() throws Exception {
  7. mockMvc.perform(get("/bars")
  8. .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
  9. .andExpect(status().isOk())
  10. .andExpect(jsonPath("$.*", hasSize(1)));
  11. }
  12. }
英文:

You shouldn't use @WebMvcTest and @SpringBootTest together.

If you want to test both web layer and other layers Use @AutoConfigureMockMvc and @SpringBootTest together:

  1. @SpringBootTest
  2. @AutoConfigureMockMvc
  3. public class BarsControllerTest {
  4. @Autowired
  5. private MockMvc mockMvc;
  6. @Test
  7. public void testBars() throws Exception {
  8. mockMvc.perform(get("/bars")
  9. .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
  10. .andExpect(status().isOk())
  11. .andExpect(jsonPath("$.*", hasSize(1)));
  12. }
  13. }

Or if you only want to test web layer you can use just @WebMvcTest: note the this does not load full spring application context(It only loads web layer)

  1. @WebMvcTest
  2. public class BarsControllerTest {
  3. @Autowired
  4. private MockMvc mockMvc;
  5. @Test
  6. public void testBars() throws Exception {
  7. mockMvc.perform(get("/bars")
  8. .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
  9. .andExpect(status().isOk())
  10. .andExpect(jsonPath("$.*", hasSize(1)));
  11. }
  12. }

huangapple
  • 本文由 发表于 2020年10月21日 19:00:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64462134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定