如何创建参数捕获器Map<String, SomeCustomClass>?
我的代码遵循以下模式:
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);
}
}
而且我想测试是否调用了该方法doSomethingSubClass(mapSb),因此我需要能够检查它被调用的参数。为此,我进行了以下单元测试:
@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);
}
问题是 argCaptor 的上述初始化会产生以下错误消息:“无法从参数化类型中选择”。因此,问题是如何以正确的方式声明一个映射对象的参数捕获器的初始化,例如Map<String, SomeCustomeClass>?提前致谢!
FFIVE
莫回无
相关分类