private void executeAndVerify() throws URISyntaxException { this.restTemplate.getForObject(new URI("/person/1"), Person.class); this.mockServer.verify(); }
private void executeAndVerify() throws URISyntaxException { this.restTemplate.put(new URI("/composers"), this.people); this.mockServer.verify(); }
private void executeAndVerify() throws URISyntaxException { this.restTemplate.put(new URI("/composers"), this.people); this.mockServer.verify(); }
private void executeAndVerify(Object body) throws URISyntaxException { this.restTemplate.put(new URI("/foo"), body); this.mockServer.verify(); }
private void executeAndVerify() throws URISyntaxException { this.restTemplate.put(new URI("/composers"), people); this.mockServer.verify(); }
@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(); }
@Test public void performGetManyTimes() { String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}"; this.mockServer.expect(manyTimes(), 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.restTemplate.getForObject("/composers/{id}", Person.class, 42); this.restTemplate.getForObject("/composers/{id}", Person.class, 42); this.restTemplate.getForObject("/composers/{id}", Person.class, 42); this.mockServer.verify(); }
@Test public void resetAndReuseServer() { MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate).build(); server.expect(requestTo("/foo")).andRespond(withSuccess()); this.restTemplate.getForObject("/foo", Void.class); server.verify(); server.reset(); server.expect(requestTo("/bar")).andRespond(withSuccess()); this.restTemplate.getForObject("/bar", Void.class); server.verify(); }
@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(); }
@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(); }
@Test // SPR-14694 public void repeatedAccessToResponseViaResource() { Resource resource = new ClassPathResource("ludwig.json", this.getClass()); RestTemplate restTemplate = new RestTemplate(); restTemplate.setInterceptors(Collections.singletonList(new ContentInterceptor(resource))); MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate) .ignoreExpectOrder(true) .bufferContent() // enable repeated reads of response body .build(); mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET)) .andRespond(withSuccess(resource, MediaType.APPLICATION_JSON)); restTemplate.getForObject("/composers/{id}", Person.class, 42); mockServer.verify(); }
@Test public void resetAndReuseServerWithUnorderedExpectationManager() { MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate) .ignoreExpectOrder(true).build(); server.expect(requestTo("/foo")).andRespond(withSuccess()); this.restTemplate.getForObject("/foo", Void.class); server.verify(); server.reset(); 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(); }
@Test public void buildMultipleTimes() { MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(this.restTemplate); MockRestServiceServer server = builder.build(); server.expect(requestTo("/foo")).andRespond(withSuccess()); this.restTemplate.getForObject("/foo", Void.class); server.verify(); server = builder.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(); server = builder.build(); server.expect(requestTo("/bar")).andRespond(withSuccess()); this.restTemplate.getForObject("/bar", Void.class); server.verify(); }
@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(); }
@Test public void performGet() throws Exception { 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") ListenableFuture<ResponseEntity<Person>> ludwig = this.restTemplate.getForEntity("/composers/{id}", Person.class, 42); // We are only validating the request. The response is mocked out. // person.getName().equals("Ludwig van Beethoven") // person.getDouble().equals(1.6035) this.mockServer.verify(); }
@Test // SPR-16132 public void followUpRequestAfterFailure() { MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate).build(); server.expect(requestTo("/some-service/some-endpoint")) .andRespond(request -> { throw new SocketException("pseudo network error"); }); server.expect(requestTo("/reporting-service/report-error")) .andExpect(method(POST)).andRespond(withSuccess()); try { this.restTemplate.getForEntity("/some-service/some-endpoint", String.class); } catch (Exception ex) { this.restTemplate.postForEntity("/reporting-service/report-error", ex.toString(), String.class); } server.verify(); }
@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(); }
@Test public void verify() { this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET)) .andRespond(withSuccess("1", MediaType.TEXT_PLAIN)); this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET)) .andRespond(withSuccess("2", MediaType.TEXT_PLAIN)); this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET)) .andRespond(withSuccess("4", MediaType.TEXT_PLAIN)); this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET)) .andRespond(withSuccess("8", MediaType.TEXT_PLAIN)); @SuppressWarnings("unused") String result1 = this.restTemplate.getForObject("/number", String.class); // result1 == "1" @SuppressWarnings("unused") String result2 = this.restTemplate.getForObject("/number", String.class); // result == "2" try { this.mockServer.verify(); } catch (AssertionError error) { assertTrue(error.getMessage(), error.getMessage().contains("2 unsatisfied expectation(s)")); } }
@Test public void performGetManyTimes() throws Exception { String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}"; this.mockServer.expect(manyTimes(), 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); // We are only validating the request. The response is mocked out. // person.getName().equals("Ludwig van Beethoven") // person.getDouble().equals(1.6035) this.restTemplate.getForEntity("/composers/{id}", Person.class, 42); this.restTemplate.getForEntity("/composers/{id}", Person.class, 42); this.restTemplate.getForEntity("/composers/{id}", Person.class, 42); this.restTemplate.getForEntity("/composers/{id}", Person.class, 42); this.mockServer.verify(); }
@Test public void verify() { this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET)) .andRespond(withSuccess("1", MediaType.TEXT_PLAIN)); this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET)) .andRespond(withSuccess("2", MediaType.TEXT_PLAIN)); this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET)) .andRespond(withSuccess("4", MediaType.TEXT_PLAIN)); this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET)) .andRespond(withSuccess("8", MediaType.TEXT_PLAIN)); @SuppressWarnings("unused") ListenableFuture<ResponseEntity<String>> result = this.restTemplate.getForEntity("/number", String.class); // result == "1" result = this.restTemplate.getForEntity("/number", String.class); // result == "2" try { this.mockServer.verify(); } catch (AssertionError error) { assertTrue(error.getMessage(), error.getMessage().contains("2 unsatisfied expectation(s)")); } }