Skip to content

Instantly share code, notes, and snippets.

@victor-ferrer
Last active April 1, 2016 23:05
// Eventhough this Spring Bean is not referenced directly by the
// test, once you link it with the MockRestServerServer; all
// calls made by it will be intercepted and checked.
@Autowired
private RestTemplate restTemplate;
@Autowired
private StockPublisher publisher;
@Test
public void testPublisher() throws IOException {
String stock = "T";
// Link the Rest Template bean with our MockRestServer
MockRestServiceServer mockedServer = MockRestServiceServer.createServer(restTemplate);
// Define which request should we expect..
mockedServer.expect(requestTo(String.format(YahooFeeder.YAHOO_URL,stock)))
// ... and the response that the Mock will provide (read from a text file)
.andRespond(withSuccess(Files.readAllBytes(sampleFilePath), MediaType.TEXT_HTML));
// Get the target flux and create a logger flux from it
Flux<StockQuotation> flux = publisher.getQuotes(Arrays.asList(stock)).log();
// Reactor class that will help executing assertions over our tested flux
TestSubscriber<StockQuotation> subscriber = new TestSubscriber<>();
subscriber.bindTo(flux)
// Block until onComplete()
.await()
// Sample period contains 36 entries
.assertValueCount(36)
.assertComplete();
// Verify that all the REST operations are performed according our specs
mockedServer.verify();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment