gpt4 book ai didi

java - Hamcrest hasItem 在 ArrayList 中不匹配

转载 作者:行者123 更新时间:2023-11-30 03:58:07 25 4
gpt4 key购买 nike

我目前正在测试 hasItem() Matcher 但无济于事。请参阅下面的示例代码:

List<String> list = new ArrayList<String>();

list.add("1");
list.add("2");
list.add("3");

org.junit.Assert.assertThat(list, hasItem("3"));

它产生

java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V
at org.hamcrest.core.IsCollectionContaining.matchesSafely(IsCollectionContaining.java:31)
at org.hamcrest.core.IsCollectionContaining.matchesSafely(IsCollectionContaining.java:14)
at org.hamcrest.TypeSafeDiagnosingMatcher.matches(TypeSafeDiagnosingMatcher.java:55)
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:12)
at org.junit.Assert.assertThat(Assert.java:865)
at org.junit.Assert.assertThat(Assert.java:832)
...

最佳答案

我做了一个本地测试,使用 JUnit-4.11 和 Hamcrest-Core-1.3 对我来说效果很好:

import java.util.ArrayList;
import java.util.List;

import org.hamcrest.core.IsCollectionContaining;
import org.junit.Test;

public class Example {
@Test public void test() {
List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
list.add("3");
org.junit.Assert.assertThat(list, IsCollectionContaining.hasItem("3"));
}
}

您可以使用与我相同的库版本和导入再试一次吗?

关于java - Hamcrest hasItem 在 ArrayList 中不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22605369/

25 4 0