作爲我的單元測試的一部分,我試圖模擬Thread.class isAlive()方法返回true使用Mockito。下面是我的代碼:如何用Mockito模擬Thread.class?
final Thread mockThread = Mockito.mock(Thread.class);
Mockito.when(mockThread.isAlive()).thenReturn(true);
這是給我的第二行以下異常:
Exception in thread "main" org.mockito.exceptions.misusing.MissingMethodInvocationException: when() requires an argument which has to be 'a method call on a mock'. For example: when(mock.getArticles()).thenReturn(articles); Also, this error might show up because: 1. you stub either of: final/private/equals()/hashCode() methods. Those methods cannot be stubbed/verified. Mocking methods declared on non-public parent classes is not supported. 2. inside when() you don't call method on mock but on some other object.
我已經使用了很多的Mockito多次這樣沒有問題。有嘲笑Thread.class有問題嗎?我已經找遍了,沒有運氣。
'isAlive'是最後的方法,因爲錯誤說你不能存根那些方法 –