Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.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 尽管url工作,但Spring控制器测试失败_Java_Spring_Unit Testing_Mockito - Fatal编程技术网

Java 尽管url工作,但Spring控制器测试失败

Java 尽管url工作,但Spring控制器测试失败,java,spring,unit-testing,mockito,Java,Spring,Unit Testing,Mockito,我正在尝试测试一个基本控制器: @Autowired DAOInterface db; @RequestMapping(value = "/postdb", method = RequestMethod.GET) @ResponseBody public String postdb( @RequestParam(value = "id", required = true) String id ) { db.addEntry(id); return "Added

我正在尝试测试一个基本控制器:

@Autowired
DAOInterface db;

@RequestMapping(value = "/postdb", method = RequestMethod.GET)
@ResponseBody
public String postdb(
        @RequestParam(value = "id", required = true) String id
) {
    db.addEntry(id);
    return "Added " + id + ".";
}
这个url的工作原理与我访问它时一样,它将它添加到数据库中,然后我得到作为响应的字符串输出

我正在尝试为它创建一个简单的单元测试:

@Autowired
MockMvc mockMvc;

@MockBean
DAOInterface daoInterface;

@Test
public void shouldReturnA200() throws Exception {
    mockMvc.perform(get("/postdb?id=3"))
            .andExpect(status().isOk());
}
但是,我得到了如下结果

MockHttpServletRequest:


    HTTP Method = GET
      Request URI = /postdb
       Parameters = {id=[3]}
          Headers = {}

Handler:
             Type = org.springframework.web.servlet.resource.ResourceHttpRequestHandler

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 404
    Error message = null
          Headers = {}
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

java.lang.AssertionError: Status 
Expected :200
Actual   :404
不知道为什么每次我尝试访问它时它都会工作,但运行此测试时失败。我看不出有什么问题。可能是因为我没有使用任何标题或正式的响应主体/视图,而只是输出一个字符串

编辑=当我添加


.contextPath(“/postdb”)
。。不确定这是否正确,但当我编写另一个测试并且不包含任何请求参数时,该测试给出的是200而不是400或404

下面对我来说很好

@Autowired
DAOInterface db;

@RequestMapping(value = "/postdb", method = RequestMethod.GET)
public ResponseEntity<String> postdb(@RequestParam(required = true) String id) {
    db.addEntry(id);
    return new ResponseEntity<>("Added " + id + ".", HttpStatus.OK);
}
公共类网络控制器{

@RequestMapping(value = "/postdb", method = RequestMethod.GET)
@ResponseBody
public String postdb(@RequestParam(value = "id", required = true) String id) {
    System.out.println("idddddddddddd "+id);
    return "Added " + id + ".";
}
}

测试班是

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class FirstWebControllerTest {

    @Configuration
    static class FirstWebControllerTestConfiguration {

        @Bean
        public FirstWebController firstWebController() {
            return new FirstWebController();
        }
    }

    @Autowired
    private FirstWebController firstWebController;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        mockMvc = standaloneSetup(firstWebController).build();
    }

    @Test
    public void shouldReturnA200() throws Exception {
        mockMvc.perform(get("/postdb?id=3")).andExpect(status().isOk());
    }

}

下面对我来说很好

公共类网络控制器{

@RequestMapping(value = "/postdb", method = RequestMethod.GET)
@ResponseBody
public String postdb(@RequestParam(value = "id", required = true) String id) {
    System.out.println("idddddddddddd "+id);
    return "Added " + id + ".";
}
}

测试班是

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class FirstWebControllerTest {

    @Configuration
    static class FirstWebControllerTestConfiguration {

        @Bean
        public FirstWebController firstWebController() {
            return new FirstWebController();
        }
    }

    @Autowired
    private FirstWebController firstWebController;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        mockMvc = standaloneSetup(firstWebController).build();
    }

    @Test
    public void shouldReturnA200() throws Exception {
        mockMvc.perform(get("/postdb?id=3")).andExpect(status().isOk());
    }

}

尝试添加查询参数,如下所示:

@Test
public void shouldReturnA200() throws Exception {
    mockMvc.perform(get("/postdb).param("id", "3"))
            .andExpect(status().isOk());
}

尝试添加查询参数,如下所示:

@Test
public void shouldReturnA200() throws Exception {
    mockMvc.perform(get("/postdb).param("id", "3"))
            .andExpect(status().isOk());
}

输入错误-
posttodb
未找到。应该是
postdb
您是否尝试了使用绝对URL的测试,例如http://localhost:8080/app name/postdb?id=3?@LeonardoAlvesMachado当我写问题时,这是一个打字错误。编辑了打字错误。@Flocke尝试了这一点,但仍然得到了相同的结果。您可能需要
。contextPath(“/”)。servletPath(“/”
。请参阅输入错误-
posttodb
未找到。应该是
postdb
您是否尝试了使用绝对URL的测试,例如http://localhost:8080/app name/postdb?id=3?@LeonardoAlvesMachado当我写问题时,这是一个打字错误。编辑了打字错误。@Flocke尝试了这一点,但仍然得到了相同的结果。您可能需要
。contextPath(“/”)。servletPath(“/”
。请参阅,测试类为,测试类为