本文介绍了使用Hystrix Spring Cloud进行单元测试后备的任何样本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想测试以下情况:
- 设置
hystrix.command.default.execution.isolation .thread.timeoutInMillisecond
值设置为低值,并查看我的应用程序的行为。 - 检查使用单元测试调用的后备方法。
- Set the
hystrix.command.default.execution.isolation.thread.timeoutInMillisecond
value to a low value, and see how my application behaves. - Check my fallback method is called using Unit test.
请有人可以向我提供示例链接。
Please can someone provide me with link to samples.
推荐答案
可以在下面找到真实用法。在测试类中启用Hystrix的关键是以下两个注释:
@EnableCircuitBreaker
@EnableAspectJAutoProxy
A real usage can be found bellow. The key to enable Hystrix in the test class are these two annotations:@EnableCircuitBreaker@EnableAspectJAutoProxy
class ClipboardService {
@HystrixCommand(fallbackMethod = "getNextClipboardFallback")
public Task getNextClipboard(int numberOfTasks) {
doYourExternalSystemCallHere....
}
public Task getNextClipboardFallback(int numberOfTasks) {
return null;
}
}
@RunWith(SpringRunner.class)
@EnableCircuitBreaker
@EnableAspectJAutoProxy
@TestPropertySource("classpath:test.properties")
@ContextConfiguration(classes = {ClipboardService.class})
public class ClipboardServiceIT {
private MockRestServiceServer mockServer;
@Autowired
private ClipboardService clipboardService;
@Before
public void setUp() {
this.mockServer = MockRestServiceServer.createServer(restTemplate);
}
@Test
public void testGetNextClipboardWithBadRequest() {
mockServer.expect(ExpectedCount.once(), requestTo("https://getDocument.com?task=1")).andExpect(method(HttpMethod.GET))
.andRespond(MockRestResponseCreators.withStatus(HttpStatus.BAD_REQUEST));
Task nextClipboard = clipboardService.getNextClipboard(1);
assertNull(nextClipboard); // this should be answered by your fallBack method
}
}
这篇关于使用Hystrix Spring Cloud进行单元测试后备的任何样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!