Mockito 1.10.17中的java.lang.VerifyError [英] java.lang.VerifyError with Mockito 1.10.17
问题描述
我正在尝试将JMock替换为Mockito(1.10.17).我已经成功完成了一些单元测试,但是现在我想使用超时功能
I am trying to replace JMock with Mockito (1.10.17). I have already done some unit tests successfully, but now I want to use the timeout feature
verify(publisher, timeout(5000)).notifySubscribers(any(BecameMasterMessage.class));
我得到这个异常:
java.lang.VerifyError: (class: org/mockito/internal/verification/VerificationOverTimeImpl, method: verify signature: (Lorg/mockito/internal/verification/api/VerificationData;)V) Incompatible argument to function
at org.mockito.verification.Timeout.<init>(Timeout.java:32)
at org.mockito.verification.Timeout.<init>(Timeout.java:25)
at org.mockito.Mockito.timeout(Mockito.java:2164)
问题发生在IntelliJ和Maven中.类路径上只有1个版本的Mockito.在类路径上还存在无法删除的JMock 2.5.1,因为目前有99%的单元测试仍在使用JMock.我不知道这是否与它有关.
The issue happens in IntelliJ and with Maven. There is only 1 version of Mockito on the classpath. There is also JMock 2.5.1 on the classpath which I cannot remove since 99% of my unit tests still use JMock at this moment. I don't know if that has anything to do with it.
更新:我尝试使用JMock 2.6.0和Hamcrest 1.3,但结果是相同的.
UPDATE: I tried with JMock 2.6.0 and Hamcrest 1.3 but the result is the same.
更新2:
这有效:
Thread.sleep( 5000 );
verify( m_publisher ).notifySubscribers( any( BecameMasterMessage.class ) );
这不是:
verify(publisher, timeout(5000)).notifySubscribers(any(BecameMasterMessage.class));
更新3: 我做了一个完全相同的问题的小型测试项目:请参见 https://github.com/wimdeblauwe/mockito-verify-problem 并从IntelliJ或Maven运行它.
UPDATE 3: I have made a small test project that has the exact same problem: See https://github.com/wimdeblauwe/mockito-verify-problem and run it from IntelliJ or with Maven.
推荐答案
这里的问题是TestNG,JUnit和Mockto之间的不幸情况.要解决您的问题,您只需要向JUnit 4.0或更高版本添加依赖项(最新版本当前为4.12):
The problem here is an unfortunate constellation between TestNG, JUnit and Mockto. To fix your issue, you just need to add a dependency to JUnit 4.0 or greater (the most recent version is currently 4.12):
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
以下是详细信息:
TestNG(显然是您的测试框架)声明了对相当老的JUnit版本3.8.1的依赖. Mockito完全没有声明对JUnit的依赖,但是它使用了JUnit 4.0(!)中引入的一些JUnit类.
TestNG, which is apparently your testing framework, declares a dependency to the quite old JUnit version 3.8.1. Mockito does not declare a dependency to JUnit at all but it uses some JUnit classes that were introduced in JUnit 4.0 (!).
您的示例中的方法Mockito#timeout()
创建一个Timeout
实例,该实例又创建一个VerificationOverTimeImpl
实例.方法VerificationOverTimeImpl#verify()
处理类型为ArgumentsAreDifferent
的错误,该错误是org.junit.ComparisonFailure
的子类.
The method Mockito#timeout()
in your example creates a Timeout
instance which in turn creates an instance of VerificationOverTimeImpl
. The method VerificationOverTimeImpl#verify()
handles an error of type ArgumentsAreDifferent
which is a subclass of org.junit.ComparisonFailure
.
从JUnit版本3.8.1到4.x,ComparisonFailure
的类层次结构更改为具有AssertionError
而不是Error
作为基类.造成VerifiyError
的原因是VerificationOverTimeImpl#handleVerifyException()
需要一个AssertionError
,但是在使用JUnit 3.8.1时将使用Error
进行调用.
From JUnit version 3.8.1 to 4.x the class hierarchy of ComparisonFailure
changed to having AssertionError
instead of Error
as base class. The VerifiyError
is caused because VerificationOverTimeImpl#handleVerifyException()
requires an AssertionError
but would be invoked with an Error
when JUnit 3.8.1 is used.
这篇关于Mockito 1.10.17中的java.lang.VerifyError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!