I want to test outgoing HTTP calls from my service using MockRestServiceServer
. I got it working using following code:
@SpringBootTest
class CommentFetcherTest {
@Autowired
RestTemplate restTemplate;
@Autowired
CommentFetcher commentFetcher;
@Test
public void shouldCallExternalService(){
MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
mockServer.expect(ExpectedCount.once(), requestTo("/test/endpoint")).andExpect(method(HttpMethod.GET));
//when
commentFetcher.fetchData();
mockServer.verify();
}
}
However the problem I run into is that RestTemplate is a bean shared between all tests from suite what makes impossible to further run integration tests which should be calling external services. This results in :
java.lang.AssertionError: No further requests expected
How is it possible to use MockRestServiceServer
only for subset of tests?
I can't get rid of Dependency Injection through @Autowired
and construct my service in tests such as
RestTemplate restTemplate = new RestTemplate();
CommentFetcher commentFetcher = new CommentFetcher(restTemplate);
As this would not read properties from application.properties
necessary to proper functioning of my service.