我希望测试以下场景:
hystrix.command.default.execution.isolation.thread.timeoutInMillisecond
值设置为较低的值,并查看我的应用程序的行为方式。请有人向我提供样品链接。
答案 0 :(得分:6)
下面可以找到真正的用法。在测试类中启用Hystrix的关键是这两个注释: @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
}
}
答案 1 :(得分:1)
在你打电话给客户之前,先在你的单元测试案例中打开电路。确保调用后退。您可以从回退返回一个常量或添加一些日志语句。 重置电路。
@Test
public void testSendOrder_openCircuit() {
String order = null;
ServiceResponse response = null;
order = loadFile("/order.json");
// use this in case of feign hystrix
ConfigurationManager.getConfigInstance()
.setProperty("hystrix.command.default.circuitBreaker.forceOpen", "true");
// use this in case of just hystrix
System.setProperty("hystrix.command.default.circuitBreaker.forceOpen", "true");
response = client.sendOrder(order);
assertThat(response.getResultStatus()).isEqualTo("Fallback");
// DONT forget to reset
ConfigurationManager.getConfigInstance()
.setProperty("hystrix.command.default.circuitBreaker.forceOpen", "false");
// use this in case of just hystrix
System.setProperty("hystrix.command.default.circuitBreaker.forceOpen", "false");
}