Examples of MvcResult


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

                .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

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

    }

    @Test
    public void test9() throws Exception {
        //异常处理
        MvcResult result = mockMvc.perform(get("/user/exception")) //执行请求
                .andExpect(status().isInternalServerError()) //验证服务器内部错误
                .andReturn();

        Assert.assertTrue(IllegalArgumentException.class.isAssignableFrom(result.getResolvedException().getClass()));
    }
View Full Code Here

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

    @Test
    public void test11() throws Exception {
        //异步测试

        //Callable
        MvcResult result = mockMvc.perform(get("/user/async1?id=1&name=zhang")) //执行请求
                .andExpect(request().asyncStarted())
                .andExpect(request().asyncResult(CoreMatchers.instanceOf(User.class))) //默认会等10秒超时
                .andReturn();

View Full Code Here

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

        mockMvc = standaloneSetup(userController).build();
    }

    @Test
    public void testView() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/user/1"))
                .andExpect(MockMvcResultMatchers.view().name("user/view"))
                .andExpect(MockMvcResultMatchers.model().attributeExists("user"))
                .andDo(MockMvcResultHandlers.print())
                .andReturn();

        Assert.assertNotNull(result.getModelAndView().getModel().get("user"));
    }
View Full Code Here

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

        this.mockMvc = webAppContextSetup(this.wac).alwaysExpect(status().isOk()).build();
    }

    @Test
    void testDevice() throws Exception {
        MvcResult mvcResult =mockMvc.perform(get("/").param("device", "mobile")).andExpect(request().asyncStarted())
                .andExpect(request().asyncResult("index")).andReturn();
        mockMvc.perform(asyncDispatch(mvcResult)).andExpect(model().attribute("device", is(equalTo("mobile"))));
    }
View Full Code Here

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

                .andExpect(asyncJsonPath("$.total", is(equalTo(count)))).andDo(print());
    }

    @Test
    public void testMyinfo() throws Exception {
        MvcResult mvcResult = mockMvc.perform(post("/user/myinfo").principal(new TestingAuthenticationToken("admin", null)))
                .andExpect(request().asyncStarted()).andExpect(request().asyncResult(is(not(isEmptyOrNullString())))).andReturn();
        mvcResult = mockMvc.perform(asyncDispatch(mvcResult)).andReturn();
        Member member = (Member) mvcResult.getRequest().getAttribute("member");
        logger.debug("My account is \"{}\" and my name is {}", member.getId(), member.getName());
        assertThat("Test UserInfo error ", "admin", is(equalTo(member.getId())));
    }
View Full Code Here

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

        assertThat("Test UserInfo error ", "admin", is(equalTo(member.getId())));
    }

    @Test
    public void testUserInfo() throws Exception {
        MvcResult mvcResult = mockMvc.perform(post("/admin/user/{account}", "admin"))
                .andExpect(request().asyncStarted()).andExpect(request().asyncResult(is(not(isEmptyOrNullString())))).andReturn();
        mvcResult = mockMvc.perform(asyncDispatch(mvcResult)).andReturn();
        Member member = (Member) mvcResult.getRequest().getAttribute("member");
        logger.debug("account \"{}\" name is {}", member.getId(), member.getName());
        assertThat("Test UserInfo error ", "admin", is(equalTo(member.getId())));
    }
View Full Code Here

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

    assertThat(xpath.evaluate("/feed/icon", doc), is("http://www.example.com/favicon.ico"));
  }

  private Document doGetForDocument(String path) throws Exception {
    ResultActions resultActions = mockMvc.perform(get(path));
    MvcResult mvcResult = resultActions.andReturn();
    return getAtomFeedDocument(mvcResult);
  }
View Full Code Here

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

public class QuestionsIndexTests extends AbstractIntegrationTests {

    @Ignore
    @Test
    public void showsQuestionsIndex() throws Exception {
        MvcResult result = mockMvc.perform(get("/questions")).andExpect(status().isOk())
                .andExpect(content().contentTypeCompatibleWith("text/html")).andReturn();

        Document document = Jsoup.parse(result.getResponse().getContentAsString());
        String body = document.select("body").text();

    // header title
    assertThat(body, containsString("Spring at StackOverflow"));
View Full Code Here

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

        checkSnapshot(releases);
    }

    public List<Object> getAndCheckProjectReleases(String projectId, String expectedProjectName) throws Exception {
        MvcResult result = mockMvc.perform(
                MockMvcRequestBuilders.get("/project_metadata/" + projectId
                        + "?callback=a_function_name")).andReturn();

        String content = result.getResponse().getContentAsString();

        String functionNameRegex = "^([^(]*)\\((.*)\\);$";
        Matcher matcher = Pattern.compile(functionNameRegex).matcher(content);
        if (matcher.find()) {
            assertThat(matcher.group(1), equalTo("a_function_name"));
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.