org.springframework.test.web.servlet.MockMvc.perform()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(7.4k)|赞(0)|评价(0)|浏览(94)

本文整理了Java中org.springframework.test.web.servlet.MockMvc.perform()方法的一些代码示例,展示了MockMvc.perform()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MockMvc.perform()方法的具体详情如下:
包路径:org.springframework.test.web.servlet.MockMvc
类名称:MockMvc
方法名:perform

MockMvc.perform介绍

[英]Perform a request and return a type that allows chaining further actions, such as asserting expectations, on the result.
[中]执行一个请求并返回一个类型,该类型允许对结果链接进一步的操作,例如断言期望。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Test
public void method() throws Exception {
  Method method = SimpleController.class.getMethod("handle");
  this.mockMvc.perform(get("/")).andExpect(handler().method(method));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testContentAsBytes() throws Exception {
  this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
    .andExpect(content().bytes("Hello world!".getBytes("ISO-8859-1")));
  this.mockMvc.perform(get("/handleUtf8"))
    .andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes("UTF-8")));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testEqualTo() throws Exception {
  this.mockMvc.perform(get("/")).andExpect(cookie().value(COOKIE_NAME, "en-US"));
  this.mockMvc.perform(get("/")).andExpect(cookie().value(COOKIE_NAME, equalTo("en-US")));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testContentStringMatcher() throws Exception {
  this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
    .andExpect(content().string(containsString("world")));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testStatusInt() throws Exception {
  this.mockMvc.perform(get("/created")).andExpect(status().is(201));
  this.mockMvc.perform(get("/createdWithComposedAnnotation")).andExpect(status().is(201));
  this.mockMvc.perform(get("/badRequest")).andExpect(status().is(400));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void requestScopedService() throws Exception {
  assertTrue("request-scoped service must be a CGLIB proxy", AopUtils.isCglibProxy(this.requestScopedService));
  this.mockMvc.perform(get("/requestScopedService").requestAttr(FROM_MVC_TEST_MOCK, FROM_MVC_TEST_MOCK));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testExists() throws Exception {
  this.mockMvc.perform(post("/persons"))
    .andExpect(flash().attributeExists("one", "two", "three"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testSessionAttributeEqualTo() throws Exception {
  this.mockMvc.perform(get("/"))
    .andExpect(request().sessionAttribute("locale", Locale.UK))
    .andExpect(request().sessionAttribute("locale", equalTo(Locale.UK)));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testEqualTo() throws Exception {
  this.mockMvc.perform(get("/"))
    .andExpect(view().name("mySpecialView"))
    .andExpect(view().name(equalTo("mySpecialView")));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testAttributeHamcrestMatchers() throws Exception {
  mockMvc.perform(get("/"))
    .andExpect(model().attribute("integer", equalTo(3)))
    .andExpect(model().attribute("string", allOf(startsWith("a string"), endsWith("value"))))
    .andExpect(model().attribute("person", hasProperty("name", equalTo("a name"))));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
void springMvcTest(WebApplicationContext wac) throws Exception {
  webAppContextSetup(wac).build()
    .perform(get("/person/42").accept(MediaType.APPLICATION_JSON))
    .andExpect(status().isOk())
    .andExpect(jsonPath("$.name", is("Dilbert")));
}

代码示例来源:origin: spring-projects/spring-framework

@Test  // SPR-13079
public void deferredResultWithDelayedError() throws Exception {
  MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResultWithDelayedError", "true"))
      .andExpect(request().asyncStarted())
      .andReturn();
  this.mockMvc.perform(asyncDispatch(mvcResult))
      .andExpect(status().is5xxServerError())
      .andExpect(content().string("Delayed Error"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testRequestAttributeEqualTo() throws Exception {
  this.mockMvc.perform(get("/main/1").servletPath("/main"))
    .andExpect(request().attribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, "/{id}"))
    .andExpect(request().attribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/1"))
    .andExpect(request().attribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, equalTo("/{id}")))
    .andExpect(request().attribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, equalTo("/1")));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void multipartRequestWithSingleFileNotPresent() throws Exception {
  standaloneSetup(new MultipartController()).build()
      .perform(multipart("/multipartfile"))
      .andExpect(status().isFound());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void multipartRequestWithFileListNoMultipart() throws Exception {
  standaloneSetup(new MultipartController()).build()
      .perform(post("/multipartfilelist"))
      .andExpect(status().isFound());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void deferredResult() throws Exception {
  MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResult", "true"))
      .andExpect(request().asyncStarted())
      .andReturn();
  this.asyncController.onMessage("Joe");
  this.mockMvc.perform(asyncDispatch(mvcResult))
      .andExpect(status().isOk())
      .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
      .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void defaultViewResolver() throws Exception {
  standaloneSetup(new PersonController()).build()
    .perform(get("/person/Corea"))
      .andExpect(model().attribute("person", hasProperty("name", equalTo("Corea"))))
      .andExpect(status().isOk())
      .andExpect(forwardedUrl("person/show"));  // InternalResourceViewResolver
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void queryParameter() throws Exception {
  standaloneSetup(new PersonController()).build()
    .perform(get("/search?name=George").accept(MediaType.APPLICATION_JSON))
      .andExpect(status().isOk())
      .andExpect(content().contentType("application/json;charset=UTF-8"))
      .andExpect(jsonPath("$.name").value("George"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void saveWithErrors() throws Exception {
  this.mockMvc.perform(post("/persons"))
    .andExpect(status().isOk())
    .andExpect(forwardedUrl("persons/add"))
    .andExpect(model().size(1))
    .andExpect(model().attributeExists("person"))
    .andExpect(flash().attributeCount(0));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void saveSpecialWithErrors() throws Exception {
  this.mockMvc.perform(post("/people"))
      .andExpect(status().isOk())
      .andExpect(forwardedUrl("persons/add"))
      .andExpect(model().size(1))
      .andExpect(model().attributeExists("person"))
      .andExpect(flash().attributeCount(0));
}

相关文章