Package org.springframework.test.web.servlet

Examples of org.springframework.test.web.servlet.MvcResult


    }

    @Test
    public void shouldLoadListOfBooks() throws Exception {
        // when
        MvcResult response = mockMvc.perform(get("/books").accept(MediaType.TEXT_HTML))
                .andExpect(status().isOk())
                .andExpect(content().contentType("text/html;charset=UTF-8"))
                .andReturn();

        // then
View Full Code Here


    }

    @Test
    public void shouldLoadSingleBook() throws Exception {
        // when
        MvcResult response = mockMvc.perform(get("/book/1").accept(MediaType.TEXT_HTML))
                .andExpect(status().isOk())
                .andExpect(content().contentType("text/html;charset=UTF-8"))
                .andReturn();

        // then
View Full Code Here

                                        "    }" +
                                        "]")
                );

        // when
        MvcResult response = mockMvc.perform(get("/books").accept(MediaType.TEXT_HTML))
                .andExpect(status().isOk())
                .andExpect(content().contentType("text/html;charset=UTF-8"))
                .andReturn();

        // then
View Full Code Here

                                        "    \"isbn\": \"0691067570\"," + System.getProperty("line.separator") +
                                        "    \"publicationDate\": \"1989\"" + System.getProperty("line.separator") +
                                        "}")
                );

        MvcResult response = mockMvc.perform(get("/book/1").accept(MediaType.TEXT_HTML))
                .andExpect(status().isOk())
                .andExpect(content().contentType("text/html;charset=UTF-8"))
                .andReturn();

        BookPage bookPage = new BookPage(response);
View Full Code Here

    public void shouldReceiveDHAgreement() throws Exception {
        DHAgreementResponse dhAgreementResponse = initializeDHAgreement();
    }

    private DHAgreementResponse initializeDHAgreement() throws Exception {
        MvcResult mvcResult = mockMvc.perform(get("/keyExchange/initialize").accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andReturn();

        return objectMapper.readValue(mvcResult.getResponse().getContentAsString(), DHAgreementResponse.class);
    }
View Full Code Here

    }

    private DefaultResponse finalizeDHAgreement(FinalizeDHRequest finalizeDHRequest) throws Exception {
        String jsonFinalizeDHRequest = objectMapper.writeValueAsString(finalizeDHRequest);

        MvcResult mvcResult = mockMvc.perform(get("/keyExchange/finalize")
                        .accept(MediaType.APPLICATION_JSON)
                        .content(jsonFinalizeDHRequest)
                        .contentType(MediaType.APPLICATION_JSON)
        )
                .andExpect(status().isOk())
                .andReturn();

        return objectMapper.readValue(mvcResult.getResponse().getContentAsString(), DefaultResponse.class);
    }
View Full Code Here


    @Test
    public void test2() throws Exception {
        //找不到控制器,404测试
        MvcResult result = mockMvc.perform(get("/user2/{id}", 1)) //执行请求
                .andDo(print())
                .andExpect(status().isNotFound()) //验证控制器不存在
                .andReturn();
        Assert.assertNull(result.getModelAndView()); //自定义断言
    }
View Full Code Here

    }

    @Test
    public void test3() throws Exception {
        //得到MvcResult自己验证
        MvcResult result = mockMvc.perform(get("/user/{id}", 1))//执行请求
                .andReturn(); //返回MvcResult
        Assert.assertNotNull(result.getModelAndView().getModel().get("user")); //自定义断言
    }
View Full Code Here

                .accept(MediaType.APPLICATION_JSON)) //执行请求
                .andExpect(content().contentType(MediaType.APPLICATION_JSON)) //验证响应contentType
                .andExpect(jsonPath("$.id").value(1)); //使用Json path验证JSON 请参考http://goessner.net/articles/JsonPath/

        String errorBody = "{id:1, name:zhang}";
        MvcResult result = mockMvc.perform(post("/user")
                .contentType(MediaType.APPLICATION_JSON).content(errorBody)
                .accept(MediaType.APPLICATION_JSON)) //执行请求
                .andExpect(status().isBadRequest()) //400错误请求
                .andReturn();

        Assert.assertTrue(HttpMessageNotReadableException.class.isAssignableFrom(result.getResolvedException().getClass()))//错误的请求内容体
    }
View Full Code Here

                .andDo(print())
                .andExpect(content().contentType(MediaType.APPLICATION_XML)) //验证响应contentType
                .andExpect(xpath("/user/id/text()").string("1")); //使用XPath表达式验证XML 请参考http://www.w3school.com.cn/xpath/

        String errorBody = "<user><id>1</id><name>zhang</name>";
        MvcResult result = mockMvc.perform(post("/user")
                .contentType(MediaType.APPLICATION_XML).content(errorBody)
                .accept(MediaType.APPLICATION_XML)) //执行请求
                .andExpect(status().isBadRequest()) //400错误请求
                .andReturn();

        Assert.assertTrue(HttpMessageNotReadableException.class.isAssignableFrom(result.getResolvedException().getClass()));//错误的请求内容体
    }
View Full Code Here

TOP

Related Classes of org.springframework.test.web.servlet.MvcResult

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.