/** * 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 // 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(); }
@Before public void setup() { this.restTemplate = new RestTemplate(); this.mockServer = MockRestServiceServer.bindTo(this.restTemplate).ignoreExpectOrder(true).build(); }
/** * 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(); }
/** * 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(); }
/** * A shortcut for {@code bindTo(restGateway).build()}. * @param restGateway the REST gateway to set up for mock testing * @return the created mock server */ public static MockRestServiceServer createServer(RestGatewaySupport restGateway) { return bindTo(restGateway).build(); }
/** * 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(); }
/** * A shortcut for {@code bindTo(restGateway).build()}. * @param restGateway the REST gateway to set up for mock testing * @return the created mock server */ public static MockRestServiceServer createServer(RestGatewaySupport restGateway) { return bindTo(restGateway).build(); }
/** * Return a bound {@link MockRestServiceServer} for the given {@link RestTemplate}, * configured with {@link RootUriRequestExpectationManager} when possible. * @param restTemplate the source REST template * @param expectationManager the source {@link RequestExpectationManager} * @return a configured {@link MockRestServiceServer} */ public static MockRestServiceServer bindTo(RestTemplate restTemplate, RequestExpectationManager expectationManager) { MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(restTemplate); return builder.build(forRestTemplate(restTemplate, expectationManager)); }
@Override public void customize(RestTemplate restTemplate) { RequestExpectationManager expectationManager = createExpectationManager(); if (this.detectRootUri) { expectationManager = RootUriRequestExpectationManager .forRestTemplate(restTemplate, expectationManager); } MockRestServiceServer server = MockRestServiceServer.bindTo(restTemplate) .build(expectationManager); this.expectationManagers.put(restTemplate, expectationManager); this.servers.put(restTemplate, server); }
/** * A shortcut for {@code bindTo(asyncRestTemplate).build()}. * @param asyncRestTemplate the AsyncRestTemplate to set up for mock testing * @return the created mock server */ public static MockRestServiceServer createServer(AsyncRestTemplate asyncRestTemplate) { return bindTo(asyncRestTemplate).build(); }
@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 // SPR-16132 public void followUpRequestAfterFailure() { MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate).build(); server.expect(requestTo("/some-service/some-endpoint")) .andRespond(request -> { throw new SocketException("pseudo network error"); }); server.expect(requestTo("/reporting-service/report-error")) .andExpect(method(POST)).andRespond(withSuccess()); try { this.restTemplate.getForEntity("/some-service/some-endpoint", String.class); } catch (Exception ex) { this.restTemplate.postForEntity("/reporting-service/report-error", ex.toString(), String.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 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(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(restGateway).build()}. * @param restGateway the REST gateway to set up for mock testing * @return the created mock server */ public static MockRestServiceServer createServer(RestGatewaySupport restGateway) { return bindTo(restGateway).build(); }
/** * 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(); }