org.springframework.test.web.client.MockRestServiceServer.expect()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(9.1k)|赞(0)|评价(0)|浏览(104)

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

MockRestServiceServer.expect介绍

[英]An alternative to #expect(RequestMatcher) that also indicates how many times the request is expected to be executed.

When request expectations have an expected count greater than one, only the first execution is expected to match the order of declaration. Subsequent request executions may be inserted anywhere thereafter.
[中]#expect(RequestMatcher)的另一个选项,它还指示请求预期执行的次数。
当请求期望的计数大于1时,只有第一次执行与声明的顺序相匹配。随后的请求执行可以插入其后的任何位置。

代码示例

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

@Test
public void performGetWithResponseBodyFromFile() {
  Resource responseBody = new ClassPathResource("ludwig.json", this.getClass());
  this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
    .andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
  @SuppressWarnings("unused")
  Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
  // hotel.getId() == 42
  // hotel.getName().equals("Holiday Inn")
  this.mockServer.verify();
}

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

@Test
public void ignoreExpectOrder() {
  MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate)
      .ignoreExpectOrder(true).build();
  server.expect(requestTo("/foo")).andRespond(withSuccess());
  server.expect(requestTo("/bar")).andRespond(withSuccess());
  this.restTemplate.getForObject("/bar", Void.class);
  this.restTemplate.getForObject("/foo", Void.class);
  server.verify();
}

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

@Test
public void contentAsBytes() throws Exception {
  this.mockServer.expect(content().bytes("foo".getBytes())).andRespond(withSuccess());
  executeAndVerify("foo");
}

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

@Test
public void performGetWithResponseBodyFromFile() throws Exception {
  Resource responseBody = new ClassPathResource("ludwig.json", this.getClass());
  this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
    .andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
  @SuppressWarnings("unused")
  ListenableFuture<ResponseEntity<Person>> ludwig =
      this.restTemplate.getForEntity("/composers/{id}", Person.class, 42);
  // hotel.getId() == 42
  // hotel.getName().equals("Holiday Inn")
  this.mockServer.verify();
}

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

@Test(expected = AssertionError.class)
public void expectNeverViolated() {
  String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
  this.mockServer.expect(once(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
      .andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
  this.mockServer.expect(never(), requestTo("/composers/43")).andExpect(method(HttpMethod.GET))
      .andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
  this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
  this.restTemplate.getForObject("/composers/{id}", Person.class, 43);
}

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

@Test
public void performGet() {
  String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
  this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
    .andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
  @SuppressWarnings("unused")
  Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
  // We are only validating the request. The response is mocked out.
  // hotel.getId() == 42
  // hotel.getName().equals("Holiday Inn")
  this.mockServer.verify();
}

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

@Test
public void testStringContains() throws Exception {
  this.mockServer.expect(requestTo("/person/1"))
    .andExpect(header("Accept", containsString("json")))
    .andRespond(withSuccess(RESPONSE_BODY, MediaType.APPLICATION_JSON));
  executeAndVerify();
}

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

@Test
public void expectNever() {
  String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
  this.mockServer.expect(once(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
      .andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
  this.mockServer.expect(never(), requestTo("/composers/43")).andExpect(method(HttpMethod.GET))
      .andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
  this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
  this.mockServer.verify();
}

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

@Test
public void contentAsString() throws Exception {
  this.mockServer.expect(content().string("foo")).andRespond(withSuccess());
  executeAndVerify("foo");
}

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

@Test
public void contentType() throws Exception {
  this.mockServer.expect(content().contentType("application/json;charset=UTF-8")).andRespond(withSuccess());
  executeAndVerify(new Person());
}

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

@Test
public void contentStringStartsWith() throws Exception {
  this.mockServer.expect(content().string(startsWith("foo"))).andRespond(withSuccess());
  executeAndVerify("foo123");
}

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

@Test
public void testString() throws Exception {
  this.mockServer.expect(requestTo("/person/1"))
    .andExpect(header("Accept", "application/json, application/*+json"))
    .andRespond(withSuccess(RESPONSE_BODY, MediaType.APPLICATION_JSON));
  executeAndVerify();
}

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

@Test
public void isNumber() throws Exception {
  this.mockServer.expect(requestTo("/composers"))
    .andExpect(content().contentType("application/json;charset=UTF-8"))
    .andExpect(jsonPath("$.composers[0].someDouble").isNumber())
    .andRespond(withSuccess());
  executeAndVerify();
}

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

@Test
public void isBoolean() throws Exception {
  this.mockServer.expect(requestTo("/composers"))
    .andExpect(content().contentType("application/json;charset=UTF-8"))
    .andExpect(jsonPath("$.composers[0].someBoolean").isBoolean())
    .andRespond(withSuccess());
  executeAndVerify();
}

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

@Test
public void testXmlEqualTo() throws Exception {
  this.mockServer.expect(requestTo("/composers"))
    .andExpect(content().contentType("application/xml"))
    .andExpect(content().xml(PEOPLE_XML))
    .andRespond(withSuccess());
  executeAndVerify();
}

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

@Test
public void isString() throws Exception {
  this.mockServer.expect(requestTo("/composers"))
    .andExpect(content().contentType("application/json;charset=UTF-8"))
    .andExpect(jsonPath("$.composers[0].name").isString())
    .andRespond(withSuccess());
  executeAndVerify();
}

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

@Test
public void testHamcrestNodeMatcher() throws Exception {
  this.mockServer.expect(requestTo("/composers"))
    .andExpect(content().contentType("application/xml"))
    .andExpect(content().node(hasXPath("/people/composers/composer[1]")))
    .andRespond(withSuccess());
  executeAndVerify();
}

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

@Test
public void value() throws Exception {
  this.mockServer.expect(requestTo("/composers"))
    .andExpect(content().contentType("application/json;charset=UTF-8"))
    .andExpect(jsonPath("$.composers[0].name").value("Johann Sebastian Bach"))
    .andExpect(jsonPath("$.performers[1].name").value("Yehudi Menuhin"))
    .andRespond(withSuccess());
  executeAndVerify();
}

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

@Test
public void doesNotExist() throws Exception {
  this.mockServer.expect(requestTo("/composers"))
    .andExpect(content().contentType("application/json;charset=UTF-8"))
    .andExpect(jsonPath("$.composers[?(@.name == 'Edvard Grieeeeeeg')]").doesNotExist())
    .andExpect(jsonPath("$.composers[?(@.name == 'Robert Schuuuuuuman')]").doesNotExist())
    .andExpect(jsonPath("$.composers[4]").doesNotExist())
    .andRespond(withSuccess());
  executeAndVerify();
}

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

@Test
public void exists() throws Exception {
  this.mockServer.expect(requestTo("/composers"))
    .andExpect(content().contentType("application/json;charset=UTF-8"))
    .andExpect(jsonPath("$.composers[0]").exists())
    .andExpect(jsonPath("$.composers[1]").exists())
    .andExpect(jsonPath("$.composers[2]").exists())
    .andExpect(jsonPath("$.composers[3]").exists())
    .andRespond(withSuccess());
  executeAndVerify();
}

相关文章