Javanica: How to test async @HystrixCommand annotated methods?
When not using the annotaions testing async method is documented. https://github.com/Netflix/Hystrix/wiki/How-To-Use#asynchronous-execution
How to test such a method? One would expect to use Future.get() method, but it throws an exception.
@HystrixCommand
public Future<User> getUserByIdAsync(final String id) {
return new AsyncResult<User>() {
@Override
public User invoke() {
return userResource.getUserById(id);
}
};
}
The only - hack - is to return the AsyncResult
IMHO this is related to https://github.com/Netflix/Hystrix/issues/729 - why force to use the AsyncResult class instead of any Future
OK, i found a better solution usable in tests.
public class HystrixAsync {
public static <T> T resolve(Future<T> future) {
return ((AsyncResult<T>) future).invoke();
}
}
Testing real command.
@Test
assertThat( HystrixAsync.resolve(abc.getUserByIdAsync("abc")), is(xxx));
Mocking command.
when(abc.getUserByIdAsync).thenReturn(CompletableFuture.completedFuture(value));
@HystrixCommand
public Future