@Autowired
private UserService userService;
@Test
public void testGetUserCache() {
HystrixRequestContext context = HystrixRequestContext.initializeContext();
try {
User user = userService.getUser("1", "name");
HystrixExecutableInfo getUserByIdCommand = getLastExecutedCommand();
assertEquals("1", user.getId());
// this is the first time we've executed this command with
// the value of "1" so it should not be from cache
assertFalse(getUserByIdCommand.isResponseFromCache());
user = userService.getUser("1", "name");
assertEquals("1", user.getId());
getUserByIdCommand = getLastExecutedCommand();
// this is the second time we've executed this command with
// the same value so it should return from cache
assertTrue(getUserByIdCommand.isResponseFromCache());
} finally {
context.shutdown();
}
// start a new request context
context = HystrixRequestContext.initializeContext();
try {
User user = userService.getUser("1", "name");
HystrixExecutableInfo getUserByIdCommand = getLastExecutedCommand();
assertEquals("1", user.getId());
// this is a new request context so this
// should not come from cache
assertFalse(getUserByIdCommand.isResponseFromCache());
} finally {
context.shutdown();
}
}