@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 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(); }
@Override public void afterTestMethod(TestContext testContext) throws Exception { ApplicationContext applicationContext = testContext.getApplicationContext(); String[] names = applicationContext .getBeanNamesForType(MockRestServiceServer.class, false, false); for (String name : names) { applicationContext.getBean(name, MockRestServiceServer.class).reset(); } }