次のシナリオをテストしたいと思います。
hystrix.command.default.execution.isolation.thread.timeoutInMillisecond
値を低い値に設定し、アプリケーションの動作を確認します。- 単体テストを使用して、フォールバック メソッドが呼び出されていることを確認します。
サンプルへのリンクを教えてください。
実際の使用法は次のとおりです。テスト クラスで Hystrix を有効にする鍵は、次の 2 つの注釈です。 @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
}
}
クライアントを呼び出す直前に、単体テスト ケースで回線を事前に開きます。フォールバックが呼び出されていることを確認してください。フォールバックから定数を返すか、いくつかのログ ステートメントを追加できます。回路をリセットします。
@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");
}