Java Mock返回错误的集合

Java Mock返回错误的集合,java,junit,collections,mockito,Java,Junit,Collections,Mockito,我想返回一个带有模拟对象的填充贴图,但贴图的大小始终为空。模拟的对象“CommandLineValues options”不为空,而且布尔变量“doCleanFirst”也可以成功模拟 这是我的Testclass: @RunWith(MockitoJUnitRunner.class) public class IndexBMECatTest { @InjectMocks private IndexBMECat classUnderTest; @Mock pri

我想返回一个带有模拟对象的填充贴图,但贴图的大小始终为空。模拟的对象“CommandLineValues options”不为空,而且布尔变量“doCleanFirst”也可以成功模拟

这是我的Testclass:

@RunWith(MockitoJUnitRunner.class)
public class IndexBMECatTest {

    @InjectMocks
    private IndexBMECat classUnderTest;

    @Mock
    private CommandLineValues options;

    @Test
    public void testAccessoryItemHasNoDublicates() {

        Map<String, String> testMap = new HashMap<>();
        testMap.put("key", "value");

        when(options.getCleanFirst()).thenReturn(false);
        when(options.readWhitlist()).thenReturn(testMap);
        
        classUnderTest.run();
    }
}
@RunWith(MockitoJUnitRunner.class)
公共类索引{
@注射模拟
被测类别的私有指数;
@嘲弄
私有命令行值选项;
@试验
public void TestAccessoryItemhasNodePublicates(){
Map testMap=newhashmap();
testMap.put(“键”、“值”);
when(options.getCleanFirst())。然后返回(false);
when(options.readWhitlist())。然后返回(testMap);
classUnderTest.run();
}
}
这是我的类的构造函数,代码从这里开始,测试的方法不相关:

private boolean doCleanFirst;
private Map<String, String> whiteList;

public IndexBMECat(TransportClient client, CommandLineValues options, BMECatReader reader) throws Exception {
             
        this.doCleanFirst = options.getCleanFirst();
        this.whiteList = options.readWhitlist();
      
        if (whiteList.isEmpty()) {
            throw new Exception("Missing whiteList");
        }
    }
private boolean doCleanFirst;
私人地图白名单;
public IndexMecat(TransportClient客户端、CommandLineValues选项、BMECader读取器)引发异常{
this.doCleanFirst=options.getCleanFirst();
this.whiteList=options.readWhitlist();
if(whiteList.isEmpty()){
抛出新异常(“缺少白名单”);
}
}
我还尝试了其他变体:

  • 模拟映射和方法“isEmpty”的返回值
  • 初始化Testclass并将模拟对象交给构造函数
  • 但白名单的大小始终为0

    Thx,现在可以工作了:

    private IndexBMECat classUnderTest;
    
        @Mock
        private CommandLineValues options;
    
        @Mock
        private BMECatReader reader;
    
        @Mock
        TransportClient client;
    
        @Before
        public void setUp() throws Exception {
            Map<String, String> testMap = new HashMap<>();
            testMap.put("key", "value");
            when(options.getCleanFirst()).thenReturn(false);
            when(options.readWhitlist()).thenReturn(testMap);
            classUnderTest = new IndexBMECat(client, options, reader);
        }
    
        @Test
        public void testAccessoryItemHasNoDublicates() {
    
            classUnderTest.run();
        }
    
    private indexbecat classUnderTest;
    @嘲弄
    私有命令行值选项;
    @嘲弄
    私人BMECader阅读器;
    @嘲弄
    运输客户;
    @以前
    public void setUp()引发异常{
    Map testMap=newhashmap();
    testMap.put(“键”、“值”);
    when(options.getCleanFirst())。然后返回(false);
    when(options.readWhitlist())。然后返回(testMap);
    classUnderTest=新索引(客户端、选项、读卡器);
    }
    @试验
    public void TestAccessoryItemhasNodePublicates(){
    classUnderTest.run();
    }
    

    首先,我模拟将在构造函数中执行的方法,然后创建testclass的实例。

    IndexMecat的实例是在执行测试之前创建的,因此当构造函数执行时,还没有设置模拟。您没有成功模拟doCleanFirstSuccessfully--false只是默认的返回值。那么,当我执行我在第2点编写的操作:初始化Testclass并将模拟对象交给构造函数时,它是否应该工作?我不知道,您需要将代码添加到您的问题中。