当前位置: 首页 > 知识库问答 >
问题:

如何监视使用Callablearg实例化的对象并使用Mockito实现Closeable?

弘靖琪
2023-03-14

我正试图通过使用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
    }
}

在我的单元测试中,我尝试了下面概述的代码,但最终都在“doNot()”行之前抛出了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.

任何帮助或建议将不胜感激!

共有1个答案

夏俊杰
2023-03-14

你写了

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

你不能这样做。您需要创建一个实际的<code>CallableExecutor<code>对象,然后才能对其进行监视。这意味着将实际值传递给构造函数,而不是传递匹配器方法的输出。

matcher方法通过操作一个内部结构来工作,Mockito使用这个内部结构来存储存根和验证信息。这就是为什么你只能在存根或验证期间使用它们。

将实际的非匹配器参数传递给< code>CallableExecutor构造函数。

 类似资料:
  • 在使用监视器的餐饮哲学家实现中,为什么提出()操作两次调用test()操作?

  • 本文向大家介绍如何使用信号量实现监视器?,包括了如何使用信号量实现监视器?的使用技巧和注意事项,需要的朋友参考一下 为了使用信号量实现监视器,为每个监视器提供了一个信号量互斥锁(已初始化为1)。Wait(mutex)必须在进入监视器之前由进程执行,并且必须在离开监视器之后执行signal(mutex)。由于信令过程必须等待,直到恢复的过程离开或等待,所以引入了另一个信号量(下一步),初始化为0。信

  • 我在使用Mockito进行单元测试初始化对象时遇到了一些困难 这是我的测试代码 要测试的代码 RecTangleService、CircleService和SquareService用注释我尝试了很多选项,最终得出结论。我没有得到我错在哪里。我试着在网上搜索了很多地方,但找不到任何帮助。

  • 我有一个Express API服务器应用程序和一个React客户端应用程序,都是用Typescript实现的。我使用TypeScript接口定义了我的数据模型,我在系统的两端都使用这些接口。但是,TypeScript接口只是编译时特性,我还需要运行时类型检查,例如验证HTTP POST数据(json)是否符合定义的数据结构。

  • 本文向大家介绍Python实现决策树并且使用Graphviz可视化的例子,包括了Python实现决策树并且使用Graphviz可视化的例子的使用技巧和注意事项,需要的朋友参考一下 一、什么是决策树(decision tree)——机器学习中的一个重要的分类算法 决策树是一个类似于数据流程图的树结构:其中,每个内部节点表示一个属性上的测试,每个分支代表一个属性输出,而每个树叶结点代表类或者类的分布,

  • 问题内容: 当我创建一个说类Employee的模拟对象时。它不调用Employee对象的构造函数。我知道Mockito在内部使用CGLIb和反射,创建了一个代理类,将该类扩展为模拟。如果未调用employee的构造函数,那么如何创建employee类的模拟实例? 问题答案: Mockito使用CGLib生成类对象。但是,要实例化此类对象,它使用Objenesis http://objenesis.