2016-07-13 2 views
0

두 가지 메소드가 병렬로 실행된다는 것을 보여주기 위해 단위 테스트를 생성합니다. 이 두 가지 방법을 조롱하고 둘 다 2 초의 지연이 있습니다. 그런 다음 단위 테스트가 4보다 적게되었는지 확인합니다 (따라서 액션이 순차적으로 실행되지 않는다는 것을 확신 할 수 있습니다. 그 이유는 4 [2 * 2] 초 이상 걸릴 것이기 때문입니다).유효성 검사 메소드가 병렬로 실행됩니다.

더 좋은 방법이 있습니까?

+1

로그의 시작과 끝. 둘 다 끝나기 전에 시작해야하며, 로그 항목을 사용하여이를 체크 할 수 있습니다 (또는 공유 변수를 원자 적으로 증가 시키십시오! - 작업이 시작될 때 감소, 끝나면 감소, 최대 값을 유지하고 응답이 2인지 확인) . 등 –

+0

같은 모의의 두 가지 방법이 있습니까? –

+0

@LorenzoMurrocu 우리는 두 가지 상황을 모두 지원하고자합니다. 같은 모의와 다른 것. –

답변

1

InOrder 기능을 사용합니다.

@Test 
public void foo() { 
    MyClass mock = Mockito.mock(MyClass.class); 
    Mockito.when(mock.methodOne())thenAnswer(new Answer<ReturnType>() 
    { 

     @Override 
     public ReturnType answer(InvocationOnMock invocation) throws Throwable 
     { 
      // Implement the delay or parallel execution 
      . . . 
      mock.methodThree(); // This invocation is just a hook to intercept the end of this method execution 
      return something; 
     } 
    }); 

    Mockito.when(mock.methodTwo())thenAnswer(new Answer<ReturnType>() 
    { 

     @Override 
     public ReturnType answer(InvocationOnMock invocation) throws Throwable 
     { 
      // Implement the delay or the parallel execution 
      . . . 
      mock.methodThree(); // This invocation is just a hook to intercept the end of this method execution 
      return something; 
     } 
    }); 



    // Here there should be the call to the real method that calls the two methods in parallel: 
    // !!HERE!! 
    // mock1.methodOne(); 
    // mock2.methodTwo(); 

    InOrder inOrder = Mockito.inOrder(mock1, mock2); 
    inOrder.verify(mock1).methodOne(); //asserts that methodOne should be invoked first 
    inOrder.verify(mock2).methodTwo(); //asserts that methodTwo should be invoked after methodOne 
    inOrder.verify(mock3, Mockito.calls(2)).methodThree(); //asserts that methodThree, that is invoked at the end of methodOne, is invoked after the methodTwo invocation. These asserts together tell us that methodTwo was called during the execution of methodOne. 
} 

여러 모의 객체로도 중위를 사용할 수 있습니다 : 이 두 가지 방법이 동일한 모의에서 인 경우에 대한 예는이 예에서

@Test 
public void foo() { 
    MyClass mock1 = Mockito.mock(MyClass.class); 
    MyClass mock2 = Mockito.mock(MyClass.class); 
    OtherClass mock3 = Mockito.mock(OtherClass.class); 

    Mockito.when(mock1.methodOne())thenAnswer(new Answer<ReturnType>() 
    { 

     @Override 
     public ReturnType answer(InvocationOnMock invocation) throws Throwable 
     { 
      // Implement the delay or the parallel execution 
      . . . 
      mock3.methodThree(); // This invocation is just a hook to intercept the end of this method execution 
      return something; 
     } 
    }); 

    Mockito.when(mock2.methodTwo())thenAnswer(new Answer<ReturnType>() 
    { 

     @Override 
     public ReturnType answer(InvocationOnMock invocation) throws Throwable 
     { 
      // Implement the delay or the parallel execution 
      . . . 
      mock3.methodThree(); // This invocation is just a hook to intercept the end of this method execution 
      return something; 
     } 
    }); 


    // Here there should be the call to the real method that calls the two methods in parallel: 
    // !!HERE!! 
    // mock1.methodOne(); 
    // mock2.methodTwo(); 

    InOrder inOrder = Mockito.inOrder(mock1, mock2); 
    inOrder.verify(mock1).methodOne(); //asserts that methodOne should be invoked first 
    inOrder.verify(mock2).methodTwo(); //asserts that methodTwo should be invoked after methodOne 
    inOrder.verify(mock3, Mockito.calls(2)).methodThree(); //asserts that methodThree, that is invoked at the end of methodOne, is invoked after the methodTwo invocation. These asserts together tell us that methodTwo was called during the execution of methodOne. 
} 

을 mock3는 인스턴스 그 유일한 목적은 두 가지 방법의 실행 종료를 연결하는 것이므로 doNothing으로 조롱해야합니다. 어쩌면 당신 시나리오에서 이것은 다르게 성취 될 수 있습니다.

편집 : 이제 답을 더 잘 설명해 드리겠습니다. 공유 한 예에서는 모의가 없으므로 테스트는 쓸모가 없습니다. 코드에서, 내가 !!HERE!!을 추가하면 실제로 두 개의 조롱 된 메소드를 병렬로 호출하는 실제 메소드가 호출되어야합니다. 또는 Answer의 두 인스턴스를 병렬로 실행하도록 구현해야하지만 모의 객체 만 사용한 테스트는 유용하지 않습니다. 필자의 예제 에서처럼 inOrder는 두 번째 호출이 끝나기 전에 발생하는지 확인합니다. 첫번째 것 (내가 덧붙인 코멘트를 보라). 중위 인터페이스에 대한

추가 정보 : 모의에 http://site.mockito.org/mockito/docs/current/org/mockito/InOrder.html http://www.tutorialspoint.com/mockito/mockito_ordered_verification.htm

+0

고맙습니다. 나는 이것이 methodOne과 methodTwo가 병렬로 실행되는 것을 어떻게 검증하는지 이해하지 못한다. 설명해 주시겠습니까? –

+0

설명해 드릴 대답을 편집합니다 –

+0

지연되어 죄송합니다. –

관련 문제