2017-04-17 114 views
4

我使用鴉膽子和註釋我栲命令的方法是這樣的:如何對javanica @HystrixCommand註釋方法進行單元測試?

@HystrixCommand(groupKey="MY_GROUP", commandKey="MY_COMMAND" fallbackMethod="fallbackMethod") 
public Object getSomething(Object request) { 
.... 

而且我想單元測試我的備用方法,而不必直接打電話給他們,也就是我想打電話給@HystrixCommand註解的方法並在拋出500錯誤後讓它自然流入回退。這一切都在單元測試之外運作。

在我的單元測試中,我使用彈簧MockRestServiceServer返回500個錯誤,這部分工作正常,但Hystrix未在我的單元測試中正確初始化。在我的測試方法的開始,我有:

HystrixRequestContext context = HystrixRequestContext.initializeContext(); 
myService.myHystrixCommandAnnotatedMethod(); 

在此之後,我試圖鍵搞定任何錐命令並檢查是否有任何執行的命令,但該列表始終是空的,我用這個方法:

public static HystrixInvokableInfo<?> getHystrixCommandByKey(String key) { 
    HystrixInvokableInfo<?> hystrixCommand = null; 
    System.out.println("Current request is " + HystrixRequestLog.getCurrentRequest()); 
    Collection<HystrixInvokableInfo<?>> executedCommands = HystrixRequestLog.getCurrentRequest() 
      .getAllExecutedCommands(); 
    for (HystrixInvokableInfo<?> command : executedCommands) { 
     System.out.println("executed command is " + command.getCommandGroup().name()); 
     if (command.getCommandKey().name().equals(key)) { 
      hystrixCommand = command; 
      break; 
     } 
    } 
    return hystrixCommand; 
} 

我意識到我失去了一些東西在我的單元測試的初始化,任何人都可以點我在正確的方向上怎麼可以適當單元測試呢?

+0

嗨@Oscar你找到了解決方案嗎? –

+1

@StefanoL nope,找不到任何解決方案。我仍然希望有人有辦法做到這一點。 –

+1

我想過編寫名爲XYZHystrixTest的單獨測試類,它實際上是用Hystrix在原地啓動Spring Context的。我沒有看到任何其他解決方案來解決這個問題。 –

回答

2

雖然你不一定UNIT測試hystrix命令。有一種春季混合測試仍然很有用,我認爲在添加註釋時點空白接受功能是不正確的。我創建的測試確保斷路器在異常情況下打開。

@RunWith(SpringRunner.class) 
@SpringBootTest 
public class HystrixProxyServiceTests { 

    @MockBean 
    private MyRepo myRepo; 

    @Autowired 
    private MyService myService; 

    private static final String ID = 「1」; 

    @Before 
    public void setup() { 
     resetHystrix(); 
     openCircuitBreakerAfterOneFailingRequest(); 
    } 

    @Test 
    public void circuitBreakerClosedOnSuccess() throws IOException, InterruptedException { 

     when(myRepo.findOneById(USER_ID1)) 
     .thenReturn(Optional.of(Document.builder().build())); 

     myService.findOneById(USER_ID1); 
     HystrixCircuitBreaker circuitBreaker = getCircuitBreaker(); 
     Assert.assertTrue(circuitBreaker.allowRequest()); 

     verify(myRepo, times(1)).findOneById(
      any(String.class)); 
    } 

    @Test 
    public void circuitBreakerOpenOnException() throws IOException, InterruptedException { 

     when(myRepo.findOneById(ID)) 
      .thenThrow(new RuntimeException()); 

     try { 
      myService.findOneById(ID); 
     } catch (RuntimeException exception) { 
      waitUntilCircuitBreakerOpens(); 
      HystrixCircuitBreaker circuitBreaker = getCircuitBreaker(); 
      Assert.assertFalse(circuitBreaker.allowRequest()); 
     } 

     verify(myRepo, times(1)).findOneById(
      any(String.class)); 
    } 

    private void waitUntilCircuitBreakerOpens() throws InterruptedException { 
     Thread.sleep(1000); 
    } 

    private void resetHystrix() { 
     Hystrix.reset(); 
    } 

    private void warmUpCircuitBreaker() { 
     myService.findOneById(USER_ID1); 
    } 

    public static HystrixCircuitBreaker getCircuitBreaker() { 
     return HystrixCircuitBreaker.Factory.getInstance(getCommandKey()); 
    } 

    private static HystrixCommandKey getCommandKey() { 
     return HystrixCommandKey.Factory.asKey("findOneById"); 
    } 

    private void openCircuitBreakerAfterOneFailingRequest() { 

     ConfigurationManager.getConfigInstance(). 
      setProperty("hystrix.command.findOneById.circuitBreaker.requestVolumeThreshold", 1); 
    } 

} 

絆了我一會兒另一個小的事情是,我已經進入了默認的註解沒有一個具體的命令鍵,但是當創建命令鍵它們對方法的名稱創建這就是我已經在上面指定。對於完整的示例,我還添加了註釋以顯示我沒有指定commandKey。

@HystrixCommand 
public Optional<Document> findOneById(final String id) { 
    return this.myRepo.findOneById(id); 
} 

希望這可以幫助別人。

1

Hystrix是一個您接受的功能, 很像Spring是一個您接受的功能。 您不需要單元測試Hystrix調用回退方法的能力。

您應該通過在單元測試中直接調用fallback方法來單元測試。

也就是說, 當您希望Hystrix調用回退方法時,您可能需要測試Hystrix實際調用回退方法; 這不會是一個單元測試, 這將是一個集成測試。

儘管可以使用jUnit編寫許多集成測試,但看起來Hystrix並不想參與jUnit測試。

我建議您應該將應用程序安裝在開發和/或qa測試環境中,並通過在正在運行的系統上強制回退來測試Hystrix回退功能。

相關問題