gpt4 book ai didi

java - Mockito 验证字符串集合

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:07:19 24 4
gpt4 key购买 nike

我想验证一个准确的 Collection 是否传递给了模拟方法。这就是我尝试这样做的方式:

This is a simple example of my real code, which reproduce exactly the same problem.

import com.google.common.collect.Lists;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.hamcrest.MockitoHamcrest;

import java.util.Collection;

public class MockTest {

@Test
void mockTest() {

final Collection<String> collection = Mockito.mock(Collection.class);

collection.addAll(Lists.newArrayList("hello"));

Mockito.verify(collection).addAll(MockitoHamcrest.argThat(Matchers.contains("hello")));
}
}

但它不起作用,我得到了这个编译错误:

Error:(20, 35) java: incompatible types: inference variable T has incompatible bounds
equality constraints: java.lang.Iterable<? extends E>
lower bounds: java.util.Collection<? extends java.lang.String>,java.lang.Object

关于为什么它不起作用或我如何以不同的方式做它的任何想法?

最佳答案

你能试试下面的代码吗

@Test
public void mockTest() {

final Collection<String> collection = Mockito.mock(Collection.class);

collection.addAll(Lists.newArrayList("hello"));

Mockito.verify(collection).addAll((Collection<? extends String>) MockitoHamcrest.argThat(Matchers.contains("hello")));
}

关于java - Mockito 验证字符串集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53043801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com