6

次のシナリオをテストしたいと思います。

  1. hystrix.command.default.execution.isolation.thread.timeoutInMillisecond値を低い値に設定し、アプリケーションの動作を確認します。
  2. 単体テストを使用して、フォールバック メソッドが呼び出されていることを確認します。

サンプルへのリンクを教えてください。

4

2 に答える 2

8

実際の使用法は次のとおりです。テスト クラスで 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
        }
    }
于 2017-08-22T12:45:29.920 に答える
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");

}
于 2016-09-07T20:10:58.110 に答える