@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(); }
@Before public void setup() { List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new StringHttpMessageConverter()); converters.add(new MappingJackson2HttpMessageConverter()); this.restTemplate = new RestTemplate(); this.restTemplate.setMessageConverters(converters); this.mockServer = MockRestServiceServer.createServer(this.restTemplate); }
@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 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(); }
/** * Set up an expectation for a single HTTP request. The returned * {@link ResponseActions} can be used to set up further expectations as * well as to define the response. * <p>This method may be invoked any number times before starting to make * request through the underlying {@code RestTemplate} in order to set up * all expected requests. * @param matcher request matcher * @return a representation of the expectation */ public ResponseActions expect(RequestMatcher matcher) { return expect(ExpectedCount.once(), matcher); }
@Test public void testWithResponseTypeExpressionSetAsClass() throws Exception { this.mockServer = MockRestServiceServer.createServer(this.restTemplateWithConverters); this.mockServer.expect(requestTo("/testApps/outboundResponse")) .andExpect(method(HttpMethod.POST)) .andRespond(withSuccess(HttpMethod.POST.name(), MediaType.TEXT_PLAIN)); this.resTypeExpressionSetSerializationChannel.send(new GenericMessage<Class<?>>(String.class)); Message<?> message = this.replyChannel.receive(5000); assertNotNull(message); assertTrue(message.getPayload() instanceof String); this.mockServer.verify(); }
@Before public void setUp(){ notificationsTemplate = new RestTemplate(); mockNotificationsServer = MockRestServiceServer.createServer(notificationsTemplate); response.put("resources", resources); mockNotificationsServer.expect(requestTo("http://notifications.example.com/registration")) .andExpect(method(PUT)) .andExpect(jsonPath("$.source_description").value("CF_Identity"))
private void executeAndVerify() throws URISyntaxException { this.restTemplate.getForObject(new URI("/person/1"), Person.class); 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); }
/** * A shortcut for {@code bindTo(restTemplate).build()}. * @param restTemplate the RestTemplate to set up for mock testing * @return the mock server */ public static MockRestServiceServer createServer(RestTemplate restTemplate) { return bindTo(restTemplate).build(); }
@Override public MockRestServiceServer build(RequestExpectationManager manager) { MockRestServiceServer server = new MockRestServiceServer(manager); MockClientHttpRequestFactory factory = server.new MockClientHttpRequestFactory(); if (this.restTemplate != null) { if (this.bufferContent) { this.restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(factory)); } else { this.restTemplate.setRequestFactory(factory); } } if (this.asyncRestTemplate != null) { this.asyncRestTemplate.setAsyncRequestFactory(factory); } return server; } }
@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 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(); }
private void executeAndVerify() throws URISyntaxException { this.restTemplate.put(new URI("/composers"), people); this.mockServer.verify(); }
/** * A shortcut for {@code bindTo(asyncRestTemplate).build()}. * @param asyncRestTemplate the AsyncRestTemplate to set up for mock testing * @return the created mock server * @deprecated see deprecation notice on * {@link org.springframework.web.client.AsyncRestTemplate} itself */ @Deprecated public static MockRestServiceServer createServer(org.springframework.web.client.AsyncRestTemplate asyncRestTemplate) { return bindTo(asyncRestTemplate).build(); }
@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 contentAsBytes() throws Exception { this.mockServer.expect(content().bytes("foo".getBytes())).andRespond(withSuccess()); executeAndVerify("foo"); }
@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(); }
@Before public void setup() { List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new StringHttpMessageConverter()); converters.add(new MappingJackson2HttpMessageConverter()); this.restTemplate = new RestTemplate(); this.restTemplate.setMessageConverters(converters); this.mockServer = MockRestServiceServer.createServer(this.restTemplate); }
private void executeAndVerify() throws URISyntaxException { this.restTemplate.put(new URI("/composers"), this.people); this.mockServer.verify(); }