@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 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 contentAsBytes() throws Exception { this.mockServer.expect(content().bytes("foo".getBytes())).andRespond(withSuccess()); executeAndVerify("foo"); }
@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(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 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 public void testStringContains() throws Exception { this.mockServer.expect(requestTo("/person/1")) .andExpect(header("Accept", containsString("json"))) .andRespond(withSuccess(RESPONSE_BODY, MediaType.APPLICATION_JSON)); executeAndVerify(); }
@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 contentAsString() throws Exception { this.mockServer.expect(content().string("foo")).andRespond(withSuccess()); executeAndVerify("foo"); }
@Test public void contentType() throws Exception { this.mockServer.expect(content().contentType("application/json;charset=UTF-8")).andRespond(withSuccess()); executeAndVerify(new Person()); }
@Test public void contentStringStartsWith() throws Exception { this.mockServer.expect(content().string(startsWith("foo"))).andRespond(withSuccess()); executeAndVerify("foo123"); }
@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(); }
@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(); }
@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(); }
@Test public void testXmlEqualTo() throws Exception { this.mockServer.expect(requestTo("/composers")) .andExpect(content().contentType("application/xml")) .andExpect(content().xml(PEOPLE_XML)) .andRespond(withSuccess()); executeAndVerify(); }
@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(); }
@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(); }
@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(); }
@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(); }
@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(); }