Mockito – 验证一个方法被调用了两次,分别使用了两个不同的参数。

huangapple go评论80阅读模式
英文:

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&lt;com.ws.Request&gt; request = ArgumentCaptor.forClass(com.ws.Request.class);
Mockito.verify(dao, Mockito.times(1)).save(request.capture());

ArgumentCaptor&lt;com.ws.Response&gt; response = ArgumentCaptor.forClass(com.ws.Response.class);
Mockito.verify(dao, Mockito.times(1)).save(response.capture());

And the DAO method:

@Transactional
Public &lt;T&gt; T save(final T it) {
    saveOrUpdate(it);
}

Error received:

org.mockito.exceptions.verification.TooManyActualInvocations: 
dao.save(&lt;Capturing argument&gt;);
Wanted 1 time:
-&gt; at com.ws.testclass(TestClass.java:296)
But was 2 times:
-&gt; at com.ws.mainclass.lambda$saveRequestAndResponse$78(MainClass.java:200)
-&gt; 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&lt;Object&gt; parameters = ArgumentCaptor.forClass(Object.class);
Mockito.verify(dao, Mockito.times(2)).save(parameters.capture());
List&lt;Object&gt; 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(&quot;was added first&quot;);
 singleMock.add(&quot;was added second&quot;);

 //create an inOrder verifier for a single mock
 InOrder inOrder = inOrder(singleMock);

 //following will make sure that add is first called with &quot;was added first, then with &quot;was added second&quot;
 inOrder.verify(singleMock).add(&quot;was added first&quot;);
 inOrder.verify(singleMock).add(&quot;was added second&quot;);

huangapple
  • 本文由 发表于 2020年9月5日 06:43:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63748791.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定