我試圖弄清楚如何通過使用 Mockito 并監視物件來跳過單元測驗中的非靜態 void 方法呼叫。
有問題的課程是:
public class CallableExecutor<T> implements Closeable {
public CallableExecutor( String s, Callable<T> c ) {
this.s = s;
this.c = c;
}
public void methodWeAreTryingToSkip( String string ) {
// some logic
}
}
我試圖單元測驗的方法是:
public String myMethodThatIsBeingUnitTested( args ) {
AtomicReference<String> id = new AtomicReference<>();
try ( CallableExecutor<String> executor = new CallableExecutor<>(
someString, () -> { id.set( someMethodCall ) );
return id.get();
} ) ) {
executor.methodWeAreTryingToSkip( string );
}
catch ( Exception e ) {
// exception handling
}
}
在我的單元測驗中,我嘗試了下面列出的代碼,但最終都在“doNothing()”行之前拋出了 Mockito 錯誤(底部有詳細的錯誤):
CallableExecutor<String> mockExecutor = spy( new CallableExecutor<>( any(), any() ) );
doNothing().when( mockExecutor ).methodWeAreTryingToSkip( anyString() );
Callable callable = mock( Callable.class );
CallableExecutor<String> mockExecutor = spy( new CallableExecutor<>( anyString(), eq( callable ) ) );
doNothing().when( mockExecutor ).methodWeAreTryingToSkip( anyString() );
錯誤:
Misplaced or misused argument matcher detected here
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(any());
verify(mock).someMethod(contains("foo"))
This message may appear after an NullPointerException if the last matcher is returning an object
like any() but the stubbed method signature expect a primitive argument, in this case,
use primitive alternatives.
when(mock.get(any())); // bad use, will raise NPE
when(mock.get(anyInt())); // correct usage use
Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(any());
verify(mock).someMethod(contains("foo"))
This message may appear after an NullPointerException if the last matcher is returning an object
like any() but the stubbed method signature expect a primitive argument, in this case,
use primitive alternatives.
when(mock.get(any())); // bad use, will raise NPE
when(mock.get(anyInt())); // correct usage use
Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.
任何幫助或建議將不勝感激!
uj5u.com熱心網友回復:
你寫了
spy( new CallableExecutor<>( any(), any() ) );
你不能這樣做。您需要先制作一個實際CallableExecutor物件,然后才能窺探它。這意味著將實際值傳遞給它的建構式,而不是傳遞匹配器方法的輸出。
匹配器方法通過操縱 Mockito 用來存盤存根和驗證資訊的內部結構來作業。這就是為什么您只能在存根或驗證期間使用它們。
將實際的非匹配器引數傳遞給CallableExecutor建構式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/483075.html
