[英]How to spy on an object that is instantiated with Callable<T> arg and implements Closeable with Mockito?
我試圖弄清楚如何通過使用 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.
任何幫助或建議將不勝感激!
你寫了
spy( new CallableExecutor<>( any(), any() ) );
你不能這樣做。 您需要先創建一個實際的CallableExecutor
對象,然后才能對其進行監視。 這意味着將實際值傳遞給它的構造函數,而不是傳遞匹配器方法的輸出。
匹配器方法通過操縱 Mockito 用來存儲存根和驗證信息的內部結構來工作。 這就是為什么您只能在存根或驗證期間使用它們。
將實際的非匹配器參數傳遞給CallableExecutor
構造函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.