【问题标题】:MockMvc test returns 404 instead of 200 without a leading "/" in urlMockMvc 测试返回 404 而不是 200,url 中没有前导“/”
【发布时间】:2018-06-22 04:43:30
【问题描述】:

使用 Spring boot 1.5.9 及以下版本:

@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@SpringBootTest
public class BigPoolControllerMockMvcTest {

    @Autowired
    private MockMvc mockMvc;

    @Before
    public void initialize() {
    }

    @Test
    public void testGetDisplayPools() throws Exception {
        this.mockMvc.perform(get(CONTROLLER_PATH + DISPLAY_POOLS_API + "?param=abc")
                .contentType(MediaType.APPLICATION_JSON_UTF8))
                .andDo(print()).andExpect(status().isOk());
    }


}

我不断得到: 警告 o.s.web.servlet.PageNotFound - 在 DispatcherServlet 中找不到带有 URI [api/v1/app/pool] 的 HTTP 请求的映射,名称为 ''

【问题讨论】:

    标签: spring spring-mvc spring-boot mockmvc


    【解决方案1】:

    原来 URL 需要以“/”开头,以便 mockmvc 请求找到它:

      @Test
        public void testGetDisplayPools() throws Exception {
            this.mockMvc.perform(get("/" + CONTROLLER_PATH + DISPLAY_POOLS_API + "?amazonRegionCode=us-west-1")
                    .contentType(MediaType.APPLICATION_JSON_UTF8))
                    .andDo(print()).andExpect(status().isOk());
        }

    前导“/”有效。感谢这篇文章:https://blmte.com/computer-internet-technology/2416430_spring-test-returning-404-instead-of-200-in-testing-rest-api-in-spring-boot

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-28
      • 2017-10-26
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      • 1970-01-01
      • 2013-10-11
      • 2013-04-08
      相关资源
      最近更新 更多