private void executeAndVerify() throws URISyntaxException { this.restTemplate.getForObject(new URI("/person/1"), Person.class); this.mockServer.verify(); }
@Test public void uri() throws InterruptedException, URISyntaxException { String result = template.getForObject(baseUrl + "/uri/{query}", String.class, "Z\u00fcrich"); assertEquals("Invalid request URI", "/uri/Z%C3%BCrich", result); result = template.getForObject(baseUrl + "/uri/query={query}", String.class, "foo@bar"); assertEquals("Invalid request URI", "/uri/query=foo@bar", result); result = template.getForObject(baseUrl + "/uri/query={query}", String.class, "T\u014dky\u014d"); assertEquals("Invalid request URI", "/uri/query=T%C5%8Dky%C5%8D", result); }
@Test public void getNoContentTypeHeader() throws UnsupportedEncodingException { byte[] bytes = template.getForObject(baseUrl + "/get/nocontenttype", byte[].class); assertArrayEquals("Invalid content", helloWorld.getBytes("UTF-8"), bytes); }
@Test public void getNoResponse() { String s = template.getForObject(baseUrl + "/get/nothing", String.class); assertNull("Invalid content", s); }
@Test public void getString() { String s = template.getForObject(baseUrl + "/{method}", String.class, "get"); assertEquals("Invalid content", helloWorld, s); }
@Test // SPR-12123 public void serverPort() { String s = template.getForObject("http://localhost:{port}/get", String.class, port); assertEquals("Invalid content", helloWorld, s); }
@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 test() throws Exception { RestTemplate template = new RestTemplate(new MockMvcClientHttpRequestFactory(this.mockMvc)); String result = template.getForObject("/foo", String.class); assertEquals("bar", result); }
@Test public void getNoContent() { String s = template.getForObject(baseUrl + "/status/nocontent", String.class); assertNull("Invalid content", s); ResponseEntity<String> entity = template.getForEntity(baseUrl + "/status/nocontent", String.class); assertEquals("Invalid response code", HttpStatus.NO_CONTENT, entity.getStatusCode()); assertNull("Invalid content", entity.getBody()); }
@Test public void getNotModified() { String s = template.getForObject(baseUrl + "/status/notmodified", String.class); assertNull("Invalid content", s); ResponseEntity<String> entity = template.getForEntity(baseUrl + "/status/notmodified", String.class); assertEquals("Invalid response code", HttpStatus.NOT_MODIFIED, entity.getStatusCode()); assertNull("Invalid content", entity.getBody()); }
@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 // 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 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 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 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(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); }
@Test public void getForObject() throws Exception { String expected = "Hello World"; mockTextPlainHttpMessageConverter(); HttpHeaders requestHeaders = new HttpHeaders(); mockSentRequest(GET, "http://example.com", requestHeaders); mockResponseStatus(HttpStatus.OK); mockTextResponseBody("Hello World"); String result = template.getForObject("http://example.com", String.class); assertEquals("Invalid GET result", expected, result); assertEquals("Invalid Accept header", MediaType.TEXT_PLAIN_VALUE, requestHeaders.getFirst("Accept")); verify(response).close(); }
@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(expected = AssertionError.class) public void exactExpectOrder() { MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate) .ignoreExpectOrder(false).build(); server.expect(requestTo("/foo")).andRespond(withSuccess()); server.expect(requestTo("/bar")).andRespond(withSuccess()); this.restTemplate.getForObject("/bar", Void.class); }
@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(); }