如何在 java 中的 mockito 中为 Map 对象创建参数捕获器?

如何创建参数捕获器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>?提前致谢!


www说
浏览 163回答 2
2回答

FFIVE

你可以这样做:与@SuppressWarnings("未选中")&nbsp; @Test&nbsp; @SuppressWarnings("unchecked")&nbsp; void TestDoSomething(){&nbsp; &nbsp; SubClass sb = mock(SubClass.class);&nbsp; &nbsp; Example ex = new Example(sb);&nbsp; &nbsp; ArgumentCaptor<Map<String, CompoundClass>> argCaptor = ArgumentCaptor.forClass(Map.class);&nbsp; &nbsp; ex.doSomeThing();&nbsp; &nbsp; verify(sb).doSomeThingSubClass(argCaptor.capture());&nbsp; &nbsp; System.out.println(argCaptor.getValue().get("x").a);&nbsp; }或使用 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 {&nbsp; @Captor&nbsp; private ArgumentCaptor<Map<String, CompoundClass>> argCaptor;&nbsp; @Test&nbsp; void TestDoSomething2(){&nbsp; &nbsp; SubClass sb = mock(SubClass.class);&nbsp; &nbsp; Example ex = new Example(sb);&nbsp; &nbsp; ex.doSomeThing();&nbsp; &nbsp; verify(sb).doSomeThingSubClass(argCaptor.capture());&nbsp; &nbsp; System.out.println(argCaptor.getValue().get("x").a);&nbsp; }}

莫回无

可以使用 Mockito 的Captor注解来声明ArgumentCaptor.例如,以下测试编译并输出aa:@Captorprivate ArgumentCaptor<Map<String, CompoundClass>> argCaptor;@Testvoid TestDoSomehing(){&nbsp; &nbsp; MockitoAnnotations.initMocks(this);&nbsp; &nbsp; SubClass sb = mock(SubClass.class);&nbsp; &nbsp; Example ex = new Example(sb);&nbsp; &nbsp; ex.doSomeThing();&nbsp; &nbsp; verify(sb).doSomeThingSubClass(argCaptor.capture());&nbsp; &nbsp; System.out.println(argCaptor.getValue().get("x").a);}从Javadocs:使用注释的优点之一@Captor是您可以避免与捕获复杂泛型类型相关的警告。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java