@Test public void hamcrestMatchers() throws Exception { this.mockServer.expect(requestTo("/composers")) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.composers[0].name").value(equalTo("Johann Sebastian Bach"))) .andExpect(jsonPath("$.performers[1].name").value(equalTo("Yehudi Menuhin"))) .andExpect(jsonPath("$.composers[0].name", startsWith("Johann"))) .andExpect(jsonPath("$.performers[0].name", endsWith("Ashkenazy"))) .andExpect(jsonPath("$.performers[1].name", containsString("di Me"))) .andExpect(jsonPath("$.composers[1].name", isIn(Arrays.asList("Johann Sebastian Bach", "Johannes Brahms")))) .andExpect(jsonPath("$.composers[:3].name", hasItem("Johannes Brahms"))) .andRespond(withSuccess()); executeAndVerify(); }
@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 contentTypeNoMatch() throws Exception { this.mockServer.expect(content().contentType("application/json;charset=UTF-8")).andRespond(withSuccess()); try { executeAndVerify("foo"); } catch (AssertionError error) { String message = error.getMessage(); assertTrue(message, message.startsWith("Content type expected:<application/json;charset=UTF-8>")); } }
@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(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 testNodeCount() throws Exception { this.mockServer.expect(requestTo("/composers")) .andExpect(content().contentType("application/xml")) .andExpect(xpath("/ns:people/composers/composer", NS).nodeCount(4)) .andExpect(xpath("/ns:people/performers/performer", NS).nodeCount(2)) .andExpect(xpath("/ns:people/composers/composer", NS).nodeCount(equalTo(4))) // Hamcrest.. .andExpect(xpath("/ns:people/performers/performer", NS).nodeCount(equalTo(2))) // Hamcrest.. .andRespond(withSuccess()); executeAndVerify(); }
@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(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 contentAsBytes() throws Exception { this.mockServer.expect(content().bytes("foo".getBytes())).andRespond(withSuccess()); executeAndVerify("foo"); }
@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(); }
@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 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 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 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 contentAsString() throws Exception { this.mockServer.expect(content().string("foo")).andRespond(withSuccess()); executeAndVerify("foo"); }
@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 tokenCannotBeFetchedFromInvalidCode() { IdentityProvider<AbstractXOAuthIdentityProviderDefinition> identityProvider = getProvider(); when(provisioning.retrieveByOrigin(eq(ORIGIN), anyString())).thenReturn(identityProvider); mockUaaServer.expect(requestTo("http://localhost/oauth/token")).andRespond(withBadRequest()); assertThrows(HttpClientErrorException.class, () -> xoAuthAuthenticationManager.authenticate(xCodeToken)); }
@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 tokenCannotBeFetchedFromCodeBecauseOfServerError() { IdentityProvider<AbstractXOAuthIdentityProviderDefinition> identityProvider = getProvider(); when(provisioning.retrieveByOrigin(eq(ORIGIN), anyString())).thenReturn(identityProvider); mockUaaServer.expect(requestTo("http://localhost/oauth/token")).andRespond(withServerError()); assertThrows(HttpServerErrorException.class, () -> xoAuthAuthenticationManager.authenticate(xCodeToken)); }
@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(); }