2016-07-13 15 views
0

2つのメソッドが並行して実行されることを示すユニットテストを作成しています。これに私は2つの方法を嘲笑し、両方に2秒の遅れを持たせる。私はユニットテストが4未満だったことを確認します(したがって、アクションが4 [2 * 2]秒以上かかるため、アクションが順番に実行されないことを確認できます)。メソッドを並行して実行する

良い方法がありますか?

+1

ログの開始と終了。両方とも両方の前に開始する必要があります、そして、あなたはこれをチェックするためにログエントリを使うことができます(または共有変数をアトミックにインクリメントします - タスクが開始すると減少し、終了すると減少し、最大を追跡し、 。等 –

+0

同じモックの2つのメソッドはありますか? –

+0

@LorenzoMurrocu我々は両方の状況をサポートしたいと考えています。同じモックと別のもの。 –

答えて

1

私はInOrder機能を使用します。 この2つの方法が同じモックからのものである場合の例である:複数のモックでも順序どおりに使用することができ

@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は単なる例でありますその唯一の目的は、2つのメソッドの実行の終了をフックすることであるので、doNothingで嘲笑されるべきです。たぶんあなたのシナリオでは、これは異なって実現することができます。

EDIT: 私はここで私の答えをよく説明します:私が共有した例では、モックだけしかないのでテストは役に立たない。私が!!HERE!!を追加したコードでは、2つのモックされたメソッドを実際に並列に呼び出す実際のメソッドを呼び出す必要があります。代わりに、Answerの2つのインスタンスを並列に実行するように実装する必要がありますが、モックだけのテストは役に立ちません。 この例では、inOrderを設定して、2番目の呼び出しが最初のもの(私が加えたコメントを見てください)。順序どおりのインタフェースについて

詳細情報:モックで ​​ http://www.tutorialspoint.com/mockito/mockito_ordered_verification.htm

+0

ありがとうございます。私はこれがmethodOneとmethodTwoを並行して実行する方法を検証する方法を理解していません。あなたはそれを説明できますか? –

+0

私はそれを説明するために私の答えを編集します –

+0

遅れて申し訳ありません、編集が完了しました! –

関連する問題