2016-06-10 24 views
0

這不會編譯。我得到一個UnfinishedStubbingException。我已經閱讀了這個網站上的Mockito api和其他問題,我認爲我的語法不應該是錯誤的,但它在doAnswer(new Answer() {失敗,所以我認爲它必須是錯的,但我不知道在哪裏。謝謝。Mockito Java doAnswer

doAnswer(new Answer() { 
      @Override 
      public Object answer(InvocationOnMock invocation) throws Throwable { 
       Object[] args = invocation.getArguments(); 
       ((CrestronNioSocketHandler.NioEventReceiver) args[0]).onDataReceived(new byte[wantedNumber]); 
       return null; 
      } 
     }).when(mockedChannel.read(any(ByteBuffer.class))); 
+1

如果你得到一個異常,那麼你運行它。如果你運行它,那麼它會編譯。 –

回答

2

它應該是這樣的

doAnswer(new Answer() { 
      @Override 
      public Object answer(InvocationOnMock invocation) throws Throwable { 
       Object[] args = invocation.getArguments(); 
       ((CrestronNioSocketHandler.NioEventReceiver) args[0]).onDataReceived(new byte[wantedNumber]); 
       return null; 
      } 
     }).when(mockedChannel).read(any(ByteBuffer.class)); 

檢查this question大約有磕碰的Mockito不同的方式。