Examples of MvcResult


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

      String methodName = statusToMethodName(status);
      Method method = StatusResultMatchers.class.getMethod(methodName);
      try {
        ResultMatcher matcher = (ResultMatcher) ReflectionUtils.invokeMethod(method, resultMatchers);
        try {
          MvcResult mvcResult = new StubMvcResult(new MockHttpServletRequest(), null, null, null, null, null, response);
          matcher.match(mvcResult);
        }
        catch (AssertionError error) {
          failures.add(error);
        }
View Full Code Here

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

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

    }

    @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

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

                                        "    }" +
                                        "]")
                );

        // 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

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

                                        "    \"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

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

    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

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

    }

    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

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


    @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

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

    }

    @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

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

                .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
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.