java – Mockito Problems – InvalidUseOfMatchersException
发布时间:2020-12-15 04:43:48 所属栏目:Java 来源:网络整理
导读:我正在使用mockito进行测试,但我遇到了这个问题: org.mockito.exceptions.misusing.InvalidUSEOfMatchersException: Invalid use of argument matchers!2 matchers expected,1 recorded:- at cl.gps.tms.planifications.planification.test.PlanificationCr
|
我正在使用mockito进行测试,但我遇到了这个问题:
org.mockito.exceptions.misusing.InvalidUSEOfMatchersException:
Invalid use of argument matchers!
2 matchers expected,1 recorded:
-> at cl.gps.tms.planifications.planification.test.PlanificationCreateTest.setUp(PlanificationCreateTest.java:68)
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(),"raw String");
When using matchers,all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(),eq("String by matcher"));
...
PlanificationCreateTest使用SimpleQueryBus创建一个通用查询,其中de first参数指示返回的对象类型,第二个参数是查询的过滤器. 我想要存根SimpleQueryBus类(外部库)返回null(仅限现在) SimpleQueryBus代码 public class SimpleQueryBus implements QueryBus {
public <T,R> R handle(Class<R> clazz,T query) throws Exception {
...
}
}
我的测试代码 public class PlanificationCreateTest {
private QueryBus queryBus;
@Before
public void setUp() throws Exception {
queryBus = Mockito.mock(SimpleQueryBus.class);
when(queryBus.handle(VehicleCollection.class,any(GetVehicle.class))).thenAnswer(null);
....
}
}
更新(已解决): public class PlanificationCreateTest {
private QueryBus queryBus;
@Before
public void setUp() throws Exception {
queryBus = Mockito.mock(SimpleQueryBus.class);
// first example
when(queryBus.handle(any(Class.class),isA(VehicleAvailable.class))).thenReturn(null);
// second example
vehicle = new VehicleCollection("001","name","tag","brand","model");
when(queryBus.handle(any(Class.class),isA(GetVehicle.class))).thenReturn(vehicle);
....
}
}
谢谢… 解决方法
发生这种情况是因为您使用any()以及Class< VehicleCollection>类型的实参参数VehicleCollection.class.
改变就像下面一样,你应该没问题: when(queryBus.handle(any(VehicleCollection.class),any(GetVehicle.class))).thenAnswer(null); Mockito解释了原因:http://mockito.googlecode.com/svn/tags/1.7/javadoc/org/mockito/Matchers.html
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
