Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
Spring mvc 如何测试POST-spring mvc_Spring Mvc_Spring 3_Spring Test_Spring Test Mvc - Fatal编程技术网

Spring mvc 如何测试POST-spring mvc

Spring mvc 如何测试POST-spring mvc,spring-mvc,spring-3,spring-test,spring-test-mvc,Spring Mvc,Spring 3,Spring Test,Spring Test Mvc,我的问题是怎么称呼这个。我可以 MyObject o = new MyObject(); myController.save(o, "value"); 但这不是我想做的。我希望MyObject位于请求后正文中?如何做到这一点 @Requestmapping(value="/save/{value}", method=RequestMethod.POST) public void post(@Valid MyObject o, @PathVariable String value{ ob

我的问题是怎么称呼这个。我可以

MyObject o = new MyObject();
myController.save(o, "value");
但这不是我想做的。我希望MyObject位于请求后正文中?如何做到这一点

@Requestmapping(value="/save/{value}", method=RequestMethod.POST)
public void post(@Valid MyObject o, @PathVariable String value{
    objectService.save(o);
}
我要说的是单元测试

编辑:


您需要使用RequestBody:

@Requestmapping(value="/save/{value}", method=RequestMethod.POST)
public void post(@RequestBody MyObject o, @PathVariable String value{
   objectService.save(o);
}

有关请求正文文档的一般信息:

请参阅下面的示例代码,该代码演示了如何使用junit和spring测试对控制器进行单元测试

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({
        DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class,
        TransactionalTestExecutionListener.class })
@Transactional
@ContextConfiguration(locations = {
    "classpath:rest.xml"
    })
public class ControllerTest{
    private MockHttpServletRequest request;
    private MockHttpServletResponse response;



    @Autowired
    private RequestMappingHandlerAdapter handlerAdapter;

    @Autowired
    private RequestMappingHandlerMapping handlerMapping;

    @Before
    public void setUp() throws Exception
    {
        this.request = new MockHttpServletRequest();
        request.setContentType("application/json");
        this.response = new MockHttpServletResponse();
    }

    @Test
    public void testPost(){
        request.setMethod("POST");
        request.setRequestURI("/save/test");  //replace test with any value

        final ModelAndView mav;
        Object handler;

        try{
                MyObject o = new MyObject();
                //set values
                //Assuming the controller consumes json
                ObjectMapper mapper = new ObjectMapper();
                //set o converted as JSON to the request body
                //request.setContent(mapper.writeValueAsString(o).getBytes());
                request.setAttribute("attribute_name", o); //in case you are trying to set a model attribute. 
                handler = handlerMapping.getHandler(request).getHandler();
                mav = handlerAdapter.handle(request, response, handler);
                Assert.assertEquals(200, response.getStatus());
                //Assert other conditions.
            }
        catch (Exception e) 
            {

            } 
    }
}

好啊也许…但是假装我在用它。我如何进行单元测试?非常感谢。一定是json吗?我必须使用requestbody吗?它可以转换为控制器接受的任何类型。形成数据,获取字节并将其设置为请求。这样我就可以执行request.setContent(o)?您需要将o转换为控制器接受的消息类型之一。控制器接受的消息类型是什么?与JSON/XML类似?如果您试图接受模型属性,则需要修改控制器,如(@modeldattribute(“myobject”)myobject o、@PathVariable String value),然后在单元测试中可以将myobject设置为参数,如request.setAttribute(“myobject”,o)。
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({
        DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class,
        TransactionalTestExecutionListener.class })
@Transactional
@ContextConfiguration(locations = {
    "classpath:rest.xml"
    })
public class ControllerTest{
    private MockHttpServletRequest request;
    private MockHttpServletResponse response;



    @Autowired
    private RequestMappingHandlerAdapter handlerAdapter;

    @Autowired
    private RequestMappingHandlerMapping handlerMapping;

    @Before
    public void setUp() throws Exception
    {
        this.request = new MockHttpServletRequest();
        request.setContentType("application/json");
        this.response = new MockHttpServletResponse();
    }

    @Test
    public void testPost(){
        request.setMethod("POST");
        request.setRequestURI("/save/test");  //replace test with any value

        final ModelAndView mav;
        Object handler;

        try{
                MyObject o = new MyObject();
                //set values
                //Assuming the controller consumes json
                ObjectMapper mapper = new ObjectMapper();
                //set o converted as JSON to the request body
                //request.setContent(mapper.writeValueAsString(o).getBytes());
                request.setAttribute("attribute_name", o); //in case you are trying to set a model attribute. 
                handler = handlerMapping.getHandler(request).getHandler();
                mav = handlerAdapter.handle(request, response, handler);
                Assert.assertEquals(200, response.getStatus());
                //Assert other conditions.
            }
        catch (Exception e) 
            {

            } 
    }
}