简体   繁体   English

如何监视使用 Callable 实例化的对象<T> arg 并使用 Mockito 实现 Closeable?

[英]How to spy on an object that is instantiated with Callable<T> arg and implements Closeable with Mockito?

I am trying to figure out how to skip a non-static void method call in a unit test by using Mockito and spying on the object.我试图弄清楚如何通过使用 Mockito 并监视对象来跳过单元测试中的非静态 void 方法调用。

The class in question is:有问题的课程是:

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
  }

}

The method I am trying to unit test is:我试图单元测试的方法是:

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
    }
}

In my unit test, I have tried the code outlined below but both end up throwing Mockito errors prior to "doNothing()" line (detailed errors at the bottom):在我的单元测试中,我尝试了下面列出的代码,但最终都在“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() );

Errors:错误:

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.

Any help or advice would be appreciated!任何帮助或建议将不胜感激!

You wrote你写了

spy( new CallableExecutor<>( any(), any() ) );

You can't do this.你不能这样做。 You need to make an actual CallableExecutor object before you can spy it.您需要先创建一个实际的CallableExecutor对象,然后才能对其进行监视。 And that means passing actual values into its constructor, not passing the output of matcher methods.这意味着将实际值传递给它的构造函数,而不是传递匹配器方法的输出。

The matcher methods work by manipulating an internal structure that Mockito uses to store stubbing and verification information.匹配器方法通过操纵 Mockito 用来存储存根和验证信息的内部结构来工作。 That's why you can only use them during stubbing or verification.这就是为什么您只能在存根或验证期间使用它们。

Pass actual non-matcher arguments to the CallableExecutor constructor.将实际的非匹配器参数传递给CallableExecutor构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM