[英]How to create an argument captor for a Map object in mockito in java?
How to create an argument captor for Map? 如何为Map创建参数捕获器?
I have the code that follows the following pattern: 我有遵循以下模式的代码:
import java.util.HashMap;
import java.util.Map;
public class CompoundClass {
public CompoundClass (String a, String b){
this.a = a;
this.b = b;
}
public String a;
public String b;
}
public class SubClass {
public void doSomeThingSubClass(Map<String, CompoundClass> mapSb) {
...
}
}
public class Example {
public SubClass sb;
public Example(SubClass sb) {
this.sb = sb;
}
public void doSomeThing () {
Map<String, CompoundClass> mapSb = new HashMap<>();
mapSb.put("x", new CompoundClass("aa","bb"));
sb.doSomeThingSubClass(mapSb);
}
}
And I want to test if the method doSomethingSubClass(mapSb) was called, whereby I need to be able to check with what argument it was called. 我想测试是否调用了方法doSomethingSubClass(mapSb),我需要能够检查它被调用的参数。 For this purpose I have the following unit test: 为此,我有以下单元测试:
@Test
void TestDoSomehing(){
SubClass sb = mock(SubClass.class);
Example ex = new Example(sb);
ArgumentCaptor<Map<String, CompoundClass>> argCaptor = ArgumentCaptor.forClass(Map<String, CompoundClass>.class);
ex.doSomeThing();
verify(sb).doSomeThingSubClass(argCaptor.capture());
System.out(argCaptor.getValue().get('x').a);
}
The problem is that the above initialization of the argCaptor produces the following error message: "Cannot select from parametrized type". 问题是上面的argCaptor初始化产生以下错误信息:“无法从参数化类型中选择”。 Therefore, the question is how to declare an initialize in a correct way the argument captor for a map object like Map? 因此,问题是如何以正确的方式声明初始化像Map这样的地图对象的参数captor? Thanks in advance! 提前致谢!
You can do it either: 你可以这样做:
with @SuppressWarnings("unchecked") 与@SuppressWarnings(“未选中”)
@Test
@SuppressWarnings("unchecked")
void TestDoSomething(){
SubClass sb = mock(SubClass.class);
Example ex = new Example(sb);
ArgumentCaptor<Map<String, CompoundClass>> argCaptor = ArgumentCaptor.forClass(Map.class);
ex.doSomeThing();
verify(sb).doSomeThingSubClass(argCaptor.capture());
System.out.println(argCaptor.getValue().get("x").a);
}
or with junit5 and @Captor annotation: 或者使用junit5和@Captor注释:
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
@TestInstance(Lifecycle.PER_METHOD)
public class TestDoSomething {
@Captor
private ArgumentCaptor<Map<String, CompoundClass>> argCaptor;
@Test
void TestDoSomething2(){
SubClass sb = mock(SubClass.class);
Example ex = new Example(sb);
ex.doSomeThing();
verify(sb).doSomeThingSubClass(argCaptor.capture());
System.out.println(argCaptor.getValue().get("x").a);
}
}
You can use Mockito's Captor
annotation to declare a parameterized instance of ArgumentCaptor
. 您可以使用Mockito的Captor
注释来声明ArgumentCaptor
的参数化实例。
For example, the following test compiles and outputs aa
: 例如,以下测试编译并输出aa
:
@Captor
private ArgumentCaptor<Map<String, CompoundClass>> argCaptor;
@Test
void TestDoSomehing(){
MockitoAnnotations.initMocks(this);
SubClass sb = mock(SubClass.class);
Example ex = new Example(sb);
ex.doSomeThing();
verify(sb).doSomeThingSubClass(argCaptor.capture());
System.out.println(argCaptor.getValue().get("x").a);
}
From the Javadocs : 来自Javadocs :
One of the advantages of using
@Captor
annotation is that you can avoid warnings related capturing complex generic types. 使用@Captor
注释的一个优点是,您可以避免捕获与复杂泛型类型相关的警告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.