I'm using Spring Boot 2.0.6 and Java 10. I did the following service that only hits an external rest api using RestTemplate.
@Service
@Slf4j
public class DbApiClientImpl implements DbApiClient {
private final String URL_DELIMITER = "/";
private RestTemplate restTemplate;
private String url;
public DbApiClientImpl(
RestTemplateBuilder restTemplate,
@Value("${dbapi.namespace}") String namespace,
@Value("${dbapi.url}") String uri,
@Value("${dbapi.username}") String username,
@Value("${dbapi.password}") String password) {
this.restTemplate = restTemplate.basicAuthorization(username,
password).build();
this.url = namespace.concat(uri);
}
@Override
@Async("asyncExecutor")
public Merchant fetchMerchant(String id) {
ResponseEntity<Merchant> response =
restTemplate.getForEntity(url.concat(URL_DELIMITER).concat(id),
Merchant.class);
return response.getBody();
}
}
And the following test using MockeRestServiceServer:
@RunWith(SpringRunner.class)
@RestClientTest(value = {DbApiClient.class})
public class DbApiClientTest {
private static final String TEST_NAME = "test";
private static final String TEST_NAME_BAD_REQUEST = "test-
1";
private static final String TEST_NAME_SERVER_ERROR =
"test-2";
@Autowired DbApiClient dbApiClient;
@Value("${dbapi.namespace}")
private String namespace;
@Value("${dbapi.url}")
private String dbApiUrl;
@Autowired private MockRestServiceServer mockServer;
@Autowired private ObjectMapper objectMapper;
@Test
public void test() throws
JsonProcessingException, IOException {
Merchant mockMerchantSpec = populateFakeMerchant();
String jsonResponse =
objectMapper.writeValueAsString(mockMerchantSpec);
mockServer
.expect(manyTimes(),
requestTo(dbApiUrl.concat("/").concat(TEST_NAME)))
.andExpect(method(HttpMethod.GET))
.andRespond(withSuccess(jsonResponse,
MediaType.APPLICATION_JSON));
assertNotNull(dbApiClient.fetchMerchant(TEST_NAME));
}
The thing is that I'm getting the following exception when I run the test "No further request expected HTTP GET http://localthost... excecuted"
So seems that the @Async is borking MockerServerService response... Also, If I commented the @Async annotation everything works just fine and I get all test green.
Thanks in advance for your comments.
Update:
As per @M.Deinum's comment. I removed the CompletableFuture from the service but I'm still getting the same exception.
CompletableFuturefrom your method, that doesn't add anything. Or make your method returnFuture<Merchant>instead of what you have now. That way your calling code will wait for the response. Currently it won't wait and the test will finish (as the thread runs in the background). - M. Deinumvoidor aFutureso your calling code knows what to expect. So my guess is your code is wrong and not your test. - M. Deinum