Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring Boot MockMvc无法使用@PathVariable进行测试_Java_Spring_Unit Testing_Spring Mvc_Spring Boot - Fatal编程技术网

Java Spring Boot MockMvc无法使用@PathVariable进行测试

Java Spring Boot MockMvc无法使用@PathVariable进行测试,java,spring,unit-testing,spring-mvc,spring-boot,Java,Spring,Unit Testing,Spring Mvc,Spring Boot,使用Spring Boot,我想测试@RestController,除了尝试使用@PathParam测试请求映射外,其他一切都很好 这涉及到保存请求映射注释的接口(在本例中为TestController)!如果我删除接口,一切都很好。。。似乎是个问题 我的控制器: public interface TestController { @RequestMapping(value = "/bar/{foo}/baz", method = RequestMethod.GET) Stri

使用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”)

如果我删除控制器的接口,它会工作

有人能给我解释一下吗

Thx

您可以尝试以下方法:

mockMvc.perform(get("/test/{foo}?foo=" + "foovalue")
        .contentType("text/plain"))
        .andExpect(status().is2xxSuccessful())
        .andExpect(content().string("foovalue"));
这里有两种方法:

  • 更改终结点的URL:

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

    mockMvc.perform(get(“/test”) .param(“foo”、“Value”)) .andExpect(状态().is2xxSuccessful()) .andExpect(content().string(“foovalue”)

  • 使用正确的URL调用端点:

    mockMvc.perform(get(“/user/1568/delete”)) .andExpect(状态().is2xxSuccessful()) .andExpect(content().string(“foovalue”)


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


    请参见

    您可以将get(“/test/{foo})”替换为get(“/test/foovalue”),而不使用参数。
    .param(“foo”,“foovalue”)
    设置value@sergiu已经尝试了..是否有相同的行为…@Sam:是的,但是为什么我需要它,它应该由uri设置?@Oziris刚刚运行了您的测试,并且
    notworkingtest
    通过了。不..获取一个404@Oziriz,使用下一步更新注释:@RequestMapping(value=“/test”,method=RequestMethod.GET)不是我想要的…我的uri更复杂,这只是一个例子。比如:/user/1568/delete,其中1568是一个id@Oziris,所以只需为您的请求使用正确的URL:mockMvc.perform(get(“/user/1568/delete”).andExpect(status().is2xxsuccesful()).andExpect(content().string(“foovalue”));您可以制作一些URL生成器来为您执行此操作。已经尝试了…相同的行为。。。