@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 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 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 deleteFeedEntry() { String requestBody = "method=delete"; mockServer.expect(requestTo(fbUrl("123456_78901234"))) .andExpect(method(POST)) .andExpect(header("Authorization", "OAuth someAccessToken")).andExpect(content().string(requestBody)) .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON)); facebook.feedOperations().deletePost("123456_78901234"); mockServer.verify(); }
@Test public void testSendingMessageToEmailAddress() throws Exception { mockNotificationsServer.expect(requestTo("http://notifications.example.com/emails")) .andExpect(method(POST)) .andExpect(jsonPath("$.kind_id").value("kind-id-01")) .andExpect(jsonPath("$.to").value("user@example.com")) .andExpect(jsonPath("$.subject").value("First message")) .andExpect(jsonPath("$.html").value("<p>Message</p>")) .andRespond(withSuccess()); notificationsService.sendMessage("user@example.com", MessageType.PASSWORD_RESET, "First message", "<p>Message</p>"); mockNotificationsServer.verify(); }
private void mockToken() { String response = getIdTokenResponse(); mockUaaServer.expect(requestTo("http://localhost/oauth/token")) .andExpect(header("Authorization", "Basic " + new String(Base64.encodeBase64("identity:identitysecret".getBytes())))) .andExpect(header("Accept", "application/json")) .andExpect(content().string(containsString("grant_type=authorization_code"))) .andExpect(content().string(containsString("code=the_code"))) .andExpect(content().string(containsString("redirect_uri=http%3A%2F%2Flocalhost%2Fcallback%2Fthe_origin"))) .andExpect(content().string(containsString(("response_type=id_token")))) .andRespond(withStatus(OK).contentType(APPLICATION_JSON).body(response)); }
@Test public void testContentTypePropagation() throws Exception { this.mockServer.expect(requestTo("/testApps/outboundResponse")) .andExpect(method(HttpMethod.POST)) .andExpect(header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON.toString())) .andRespond(withSuccess(HttpMethod.POST.name(), MediaType.TEXT_PLAIN)); this.contentTypePropagationChannel .send(new GenericMessage<Map<String, String>>(Collections.singletonMap("foo", "bar"))); this.mockServer.verify(); }
@Test(expected = AssertionError.class) public void testBytesNoMatch() throws Exception { this.request.getBody().write("test".getBytes()); MockRestRequestMatchers.content().bytes("Test".getBytes()).match(this.request); }
@Test public void requestToNoMatch() throws Exception { this.request.setURI(new URI("http://foo.com/bar")); assertThrows(AssertionError.class, () -> MockRestRequestMatchers.requestTo("http://foo.com/wrong").match(this.request)); }
@Test public void headerContainsWithMissingHeader() throws Exception { AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.header("foo", containsString("baz")).match(this.request)); assertThat(error.getMessage(), containsString("but was null")); }
@Test public void methodNoMatch() throws Exception { this.request.setMethod(HttpMethod.POST); AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.method(HttpMethod.GET).match(this.request)); assertThat(error.getMessage(), containsString("expected:<GET> but was:<POST>")); }
@Test public void deleteComment() { mockServer.expect(requestTo(fbUrl("1533260333_122829644452184_587062"))) .andExpect(method(POST)) .andExpect(content().string("method=delete")) .andExpect(header("Authorization", "OAuth someAccessToken")) .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON)); facebook.commentOperations().deleteComment("1533260333_122829644452184_587062"); mockServer.verify(); }
response.put("resources", resources); mockNotificationsServer.expect(requestTo("http://notifications.example.com/registration")) .andExpect(method(PUT)) .andExpect(jsonPath("$.source_description").value("CF_Identity")) .andExpect(jsonPath("$.kinds[0].id").value("kind-id-01")) .andExpect(jsonPath("$.kinds[0].description").value("password reset")) .andExpect(jsonPath("$.kinds[0].critical").value(true)) .andRespond(withSuccess());
@Test public void testBoolean() throws Exception { String performerBooleanValue = "/ns:people/performers/performer[%s]/someBoolean"; this.mockServer.expect(requestTo("/composers")) .andExpect(content().contentType("application/xml")) .andExpect(xpath(performerBooleanValue, NS, 1).booleanValue(false)) .andExpect(xpath(performerBooleanValue, NS, 2).booleanValue(true)) .andRespond(withSuccess()); executeAndVerify(); }
@Test public void testJsonLenientMatch() throws Exception { String content = "{\n \"foo array\":[\"first\",\"second\"] , \"someExtraProperty\": \"which is allowed\" \n}"; this.request.getBody().write(content.getBytes()); MockRestRequestMatchers.content().json("{\n \"foo array\":[\"second\",\"first\"] \n}") .match(this.request); MockRestRequestMatchers.content().json("{\n \"foo array\":[\"second\",\"first\"] \n}", false) .match(this.request); }
@Test public void hasRemainingCount() { RequestExpectation expectation = new DefaultRequestExpectation(twice(), requestTo("/foo")); expectation.andRespond(withSuccess()); expectation.incrementAndValidate(); assertTrue(expectation.hasRemainingCount()); expectation.incrementAndValidate(); assertFalse(expectation.hasRemainingCount()); }
@Test public void headersWithMissingHeader() throws Exception { AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.header("foo", "bar").match(this.request)); assertThat(error.getMessage(), containsString("but was null")); }