I use javanica and annotate my hystrix command methods as follows:
@HystrixCommand(groupKey="MY_GROUP", commandKey="MY_COMMAND" fallbackMethod="fallbackMethod")
public Object getSomething(Object request) {
....
And I'm trying to combine the tests with my helper methods, without naming them directly, i.e. I would like to name an annotated method @HystrixCommandand allow it to naturally fall into the reserve after throwing a 500 error. All this works outside of unit tests.
In my unit tests, I use springs MockRestServiceServerto return 500 errors, this part works, but Hystrix does not initialize correctly in my unit tests. At the beginning of my testing method, I have:
HystrixRequestContext context = HystrixRequestContext.initializeContext();
myService.myHystrixCommandAnnotatedMethod();
After that, I try to get any hystrix command using the key and check if there are any executed commands, but the list is always empty, I use this method:
public static HystrixInvokableInfo<?> getHystrixCommandByKey(String key) {
HystrixInvokableInfo<?> hystrixCommand = null;
System.out.println("Current request is " + HystrixRequestLog.getCurrentRequest());
Collection<HystrixInvokableInfo<?>> executedCommands = HystrixRequestLog.getCurrentRequest()
.getAllExecutedCommands();
for (HystrixInvokableInfo<?> command : executedCommands) {
System.out.println("executed command is " + command.getCommandGroup().name());
if (command.getCommandKey().name().equals(key)) {
hystrixCommand = command;
break;
}
}
return hystrixCommand;
}
I understand that I have something missing in the initialization of universes, can someone point me in the right direction, how can I perform this testing correctly?
source
share