gpt4 book ai didi

java - 使用带有通用方法签名的 Mockito (java, eclipse) v1.9

转载 作者:行者123 更新时间:2023-12-01 15:19:44 25 4
gpt4 key购买 nike

我已经在 stackoverlow 上找不到这个问题的答案了。这是我可以归结为最简单的例子。

我有一个我想模拟的类,它调用了这个类


public class GetCustomerForUser extends PropertyAction<Customer>
....

属性操作:

public abstract class PropertyAction<R> extends AbstractProcessAction<R> implements
ValuedAction<R, R> {

真正的调用代码是这样的

Customer self = dispatcher.invokeTransactionless(actor, new GetCustomerForUser());
调度程序的类型为 ActionDispatcher

在调用模拟中

    protected void applyWhenClauses(ActionDispatcher dispatcher, Actor actor) throws ProcessException {
when(dispatcher.invokeTransactionless(actor, (ProcessAction<Customer>) anyObject())).thenReturn(null);
....

调度程序的类型为 ActionDispatcherMock

当我运行测试时,我得到

     org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
2 matchers expected, 1 recorded.
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"));

For more info see javadoc for Matchers class.

at com.prolog.test.mock.ActionDispatcherMock2.applyWhenClauses
(ActionDispatcherMock2.java:18)
at com.prolog.test.mock.ActionDispatcherMock.createMockInstance
(ActionDispatcherMock.java:84)
at com.prolog.test.mock.ActionDispatcherMock.createMockInstance
(ActionDispatcherMock.java:1)
at com.prolog.test.mockFactory.AbstractPrologInstanceMock.createInstanceMock
(AbstractPrologInstanceMock.java:11)

我打算为调度程序可以调用的每个类设置一个时间。

有人对我在这里做错了什么有什么智慧的话吗?

感谢您的宝贵时间。

最佳答案

错误消息表明问题所在:如果您使用任何...any,则不能使用非any匹配器:

 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"));

看看你是如何准确地完成它所说的你不能做的事情的?以及如何纠正它?

关于java - 使用带有通用方法签名的 Mockito (java, eclipse) v1.9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11118598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com