【问题标题】:Spring Boot MockMvc not working test with @PathVariable [duplicate]Spring Boot MockMvc 无法使用 @PathVariable 进行测试 [重复]
【发布时间】:2017-09-01 08:26:51
【问题描述】:

使用 Spring Boot,我想测试我的 @RestController,一切都很好,除非我尝试使用 @PathParam 测试请求映射。

这涉及到包含请求映射注解的接口(在本例中为 TestController)!如果我删除界面一切都很好......似乎是一个问题......

我的控制器:

public interface TestController {

    @RequestMapping(value = "/bar/{foo}/baz", method = RequestMethod.GET)
    String test(@PathVariable("foo") String foo);


    @RestController
    class TestControllerImpl implements TestController {

        @Override
        public String test(String foo) {
            return foo;
        }
    }

}

我的测试:

@Test
public void notworkingtest() throws Exception {

    //THIS TEST SHOULD WORK, BUT DON'T ...

    final MockMvc mockMvc = ...

    mockMvc.perform(get("/bar/{foo}/baz", "foovalue") // Or get("/bar/foovalue/baz")
            .contentType("text/plain"))
            .andExpect(status().is2xxSuccessful())
            .andExpect(content().string("foovalue"));

}

@Test
public void strangeworkingtest() throws Exception {

    //THIS TEST WORKS, BUT WHY ?!?

    final MockMvc mockMvc = ...

    mockMvc.perform(get("/bar/{foo}/baz", "WhatEverValue")
            .param("foo", "foovalue") // This param should be useless ...
            .contentType("text/plain"))
            .andExpect(status().is2xxSuccessful())
            .andExpect(content().string("foovalue"));

}

当我有 .param("foo","foovalue) 并保留 get("/bar/{foo}/baz", "WhatEverValue") ...

如果我删除控制器的接口,它就可以工作......

谁能给我解释一下?

谢谢

【问题讨论】:

  • 您可以将 get("/test/{foo})" 替换为不带参数的 get("/test/foovalue")。
  • .param("foo", "foovalue") 设置值
  • @sergiu 已经尝试 .. 做同样的行为 ...
  • @Sam :是的,但为什么我需要它,它应该由 uri 设置?
  • @Oziris 刚刚运行了您的测试,notworkingtest 确实通过了。

标签: java spring unit-testing spring-mvc spring-boot


【解决方案1】:

这里有两种方法:

  1. 更改端点的 URL:

    @RequestMapping(value = "/test", method = RequestMethod.GET)

    mockMvc.perform(get("/test") .param("foo", "Value")) .andExpect(status().is2xxSuccessful()) .andExpect(content().string("foovalue"));

  2. 使用正确的 URL 调用您的端点:

    mockMvc.perform(get("/user/1568/delete")) .andExpect(status().is2xxSuccessful()) .andExpect(content().string("foovalue"));

【讨论】:

  • 不.. 得到 404
  • @Oziriz,用下一个更新您的注释:@RequestMapping(value = "/test", method = RequestMethod.GET)
  • 不是我想要的......我的 uri 更复杂,这只是一个例子。 like : /user/1568/delete,其中 1568 是一个 id
  • @Oziris,所以只需为您的请求使用正确的 URL:mockMvc.perform(get(" /user/1568/delete")) .andExpect(status().is2xxSuccessful()) .andExpect (content().string("foovalue"));您可以制作一些 URL 构建器来为您完成。
  • 已经尝试...只是相同的行为...
【解决方案2】:

你可以试试这个:

mockMvc.perform(get("/test/{foo}?foo=" + "foovalue")
        .contentType("text/plain"))
        .andExpect(status().is2xxSuccessful())
        .andExpect(content().string("foovalue"));

【讨论】:

    【解决方案3】:

    PathVariable 与 requestParam 不同,因为 pathVariable 是 URL 的一部分。 这就是为什么.param("foo", "foovalue") 没有覆盖您的 pathVariable,因为后者正在设置 requestParam。

    @RequestParam vs @PathVariable

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-25
      • 2017-01-05
      • 2013-07-06
      • 2015-01-01
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      • 2018-04-06
      相关资源
      最近更新 更多