英文:
Mockito - Verify a method is called twice with two different params
问题
Sure, here is the translation of the provided content:
我必须验证以下方法的行为:
public void saveRequestAndResponse(request, response, additionalInfo) {
// 对 additionalInfo 进行一些处理
dao.save(request);
dao.save(response);
}
在我的测试类中:
ArgumentCaptor<com.ws.Request> request = ArgumentCaptor.forClass(com.ws.Request.class);
Mockito.verify(dao, Mockito.times(1)).save(request.capture());
ArgumentCaptor<com.ws.Response> response = ArgumentCaptor.forClass(com.ws.Response.class);
Mockito.verify(dao, Mockito.times(1)).save(response.capture());
以及 DAO 方法:
@Transactional
public <T> T save(final T it) {
saveOrUpdate(it);
}
收到的错误:
org.mockito.exceptions.verification.TooManyActualInvocations:
dao.save(<Capturing argument>);
Wanted 1 time:
-> at com.ws.testclass(TestClass.java:296)
But was 2 times:
-> at com.ws.mainclass.lambda$saveRequestAndResponse$78(MainClass.java:200)
-> at com.ws.mainclass.saveRequestAndResponse(MainClass.java:205)
我的 DAO 类中的 save() 方法使用了类型参数 T。
我如何验证对 dao.save(type)
方法的两次调用,这两次调用使用了不同的类型,比如 Request 和 Response?
英文:
I have to verify the behavior on the following method:
public void saveRequestAndResponse(request, response, additionalInfo) {
// some processing with additionalInfo
dao.save(request);
dao.save(response);
}
In my test class:
ArgumentCaptor<com.ws.Request> request = ArgumentCaptor.forClass(com.ws.Request.class);
Mockito.verify(dao, Mockito.times(1)).save(request.capture());
ArgumentCaptor<com.ws.Response> response = ArgumentCaptor.forClass(com.ws.Response.class);
Mockito.verify(dao, Mockito.times(1)).save(response.capture());
And the DAO method:
@Transactional
Public <T> T save(final T it) {
saveOrUpdate(it);
}
Error received:
org.mockito.exceptions.verification.TooManyActualInvocations:
dao.save(<Capturing argument>);
Wanted 1 time:
-> at com.ws.testclass(TestClass.java:296)
But was 2 times:
-> at com.ws.mainclass.lambda$saveRequestAndResponse$78(MainClass.java:200)
-> at com.ws.mainclass.saveRequestAndResponse(MainClass.java:205)
The save() method in my DAO class uses the type parameter T.
How do I verify two invocations on dao.save(type)
method with two different types such as Request and Response?
答案1
得分: 3
你可以使用类似这样的代码:
ArgumentCaptor<Object> parameters = ArgumentCaptor.forClass(Object.class);
Mockito.verify(dao, Mockito.times(2)).save(parameters.capture());
List<Object> values = parameters.getAllValues();
com.ws.Request req = (com.ws.Request) values.get(0);
com.ws.Response res = (com.ws.Response) values.get(1);
// 验证部分
英文:
You can use something like this:
ArgumentCaptor<Object> parameters = ArgumentCaptor.forClass(Object.class);
Mockito.verify(dao, Mockito.times(2)).save(parameters.capture());
List<Object> values= parameters.getAllValues();
com.ws.Request req= (com.ws.Request) values.get(0);
com.ws.Response res= (com.ws.Respons) values.get(1);
//Validations
答案2
得分: 1
你想要的是有序验证。根据文档:
// A. 需要按特定顺序调用方法的单个模拟对象
List singleMock = mock(List.class);
// 使用单个模拟对象
singleMock.add("首先添加");
singleMock.add("其次添加");
// 为单个模拟对象创建有序验证器
InOrder inOrder = inOrder(singleMock);
// 以下代码将确保首先使用"首先添加"调用 add 方法,然后使用"其次添加"调用 add 方法
inOrder.verify(singleMock).add("首先添加");
inOrder.verify(singleMock).add("其次添加");
英文:
What you want is in order verification. From the documentation:
// A. Single mock whose methods must be invoked in a particular order
List singleMock = mock(List.class);
//using a single mock
singleMock.add("was added first");
singleMock.add("was added second");
//create an inOrder verifier for a single mock
InOrder inOrder = inOrder(singleMock);
//following will make sure that add is first called with "was added first, then with "was added second"
inOrder.verify(singleMock).add("was added first");
inOrder.verify(singleMock).add("was added second");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论