Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么';t此代码试图使用Hamcrest';什么是编译?_Java_Unit Testing_Junit_Hamcrest_Matcher - Fatal编程技术网

Java 为什么';t此代码试图使用Hamcrest';什么是编译?

Java 为什么';t此代码试图使用Hamcrest';什么是编译?,java,unit-testing,junit,hamcrest,matcher,Java,Unit Testing,Junit,Hamcrest,Matcher,为什么这个不编译,哦,怎么办 import static org.junit.Assert.assertThat; import static org.junit.matchers.JUnitMatchers.hasItems; ArrayList<Integer> actual = new ArrayList<Integer>(); ArrayList<Integer> expected = new ArrayList<Integer>();

为什么这个不编译,哦,怎么办

import static org.junit.Assert.assertThat;
import static org.junit.matchers.JUnitMatchers.hasItems;

ArrayList<Integer> actual = new ArrayList<Integer>();
ArrayList<Integer> expected = new ArrayList<Integer>();
actual.add(1);
expected.add(2);
assertThat(actual, hasItems(expected));
import static org.junit.Assert.assertThat;
导入静态org.junit.matchers.JUnitMatchers.hasItems;
ArrayList实际值=新的ArrayList();
应为ArrayList=新的ArrayList();
增加(1);
增加(2);
资产(实际,有项目(预期));
从注释复制的错误:

cannot find symbol method assertThat(java.util.ArrayList<java.lang.Integer>, org.hamcreset.Matcher<java.lang.Iterable<java.util.ArrayList<java.lang.Integer>>>)
找不到符号方法assertThat(java.util.ArrayList,org.hamcreset.Matcher)

您正在将
ArrayList
int
进行比较。正确的比较是:

...
assertThat(actual, hasItem(2));
--编辑--

对不起,我看错了。无论如何,您想要的
hasItems
的签名是:

public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(T... elements)
--编辑2--

只需将我的评论粘贴到这里,就可以得到一个与您想要的内容等效的表达式,无需使用Hamcrest:

assertTrue(actual.containsAll(expected));

该错误消息看起来像是由javac编译器生成的。我发现过去使用hamcrest编写的代码无法在javac下编译。同样的代码可以在Eclipse编译器下很好地编译

我认为Hamcrest的泛型在泛型中使用了javac无法处理的特殊情况。

试试看

assertThat(actual, hasItems(expected.toArray(new Integer[0])));

以满足匹配者签名。没有Eclipse,因此这可能不起作用。

hasItems检查一个集合是否包含某些项,而不是两个集合是否相等,只需使用普通的相等断言即可。所以要么使用assertEquals(a,b),要么使用assertEquals

import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.is;

ArrayList<Integer> actual = new ArrayList<Integer>();
ArrayList<Integer> expected = new ArrayList<Integer>();
actual.add(1);
expected.add(2);
assertThat(actual, is(expected));
import static org.junit.Assert.assertThat;
导入静态org.hamcrest.CoreMatchers.is;
ArrayList实际值=新的ArrayList();
应为ArrayList=新的ArrayList();
增加(1);
增加(2);
(实际的、预期的)资产;
或者,使用contains匹配器,检查Iterable是否包含特定顺序的项

import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.contains;

ArrayList<Integer> actual = new ArrayList<Integer>();
actual.add(1);
actual.add(2);
assertThat(actual, contains(1, 2)); // passes
assertThat(actual, contains(3, 4)); // fails
import static org.junit.Assert.assertThat;
导入静态org.hamcrest.Matchers.contains;
ArrayList实际值=新的ArrayList();
增加(1);
增加(2);
资产(实际,包含(1,2));//通行证
资产(实际,包含(3,4));//失败
如果您不关心订单,请使用
containsInAnyOrder

ArrayList<Integer> expected = new ArrayList<Integer>();
expected.add(1);
expected.add(2);
hasItems(expected);
这将扩大到:

hasItems(1, 2);

对于这些情况,当代码在Eclipse中编译但javac显示错误时,请通过显式提供类型参数来帮助hamcrest,例如。
Matchers.hasItem()

我刚刚遇到了同样的问题,下面的技巧对我有效:

  • 使用导入静态org.hamcrest.Matchers.hasItems
  • 将hamcrest库放在类路径中junit之前(构建路径->排序和导出)

我刚刚遇到这篇文章,试图为自己修复它。给了我足够的信息来解决这个问题

通过将返回值从hasItems强制转换为(原始)匹配器,您可以为编译器提供足以说服其编译的信息,例如:

ArrayList<Integer> actual = new ArrayList<Integer>();
ArrayList<Integer> expected = new ArrayList<Integer>();
actual.add(1);
expected.add(2);
assertThat(actual, (Matcher) hasItems(expected));
ArrayList实际值=新的ArrayList();
应为ArrayList=新的ArrayList();
增加(1);
增加(2);
资产(实际,(匹配者)有项目(预期));
以防万一还有人还在受苦

编辑以添加: 正如阿伦德在下文中指出的那样,尽管投票率上升,但这个答案是错误的。正确的答案是将期望值转换为整数数组,正如hamcrest期望的那样:

    ArrayList<Integer> actual = new ArrayList<Integer>();
    ArrayList<Integer> expected = new ArrayList<Integer>();
    actual.add(1);
    expected.add(2);
    assertThat(actual, hasItems(expected.toArray(new Integer[expected.size()])));
ArrayList实际值=新的ArrayList();
应为ArrayList=新的ArrayList();
增加(1);
增加(2);
assertThat(实际,hasItems(预期的.toArray(新整数[expected.size()]));

如果尝试用更新版本替换jUnit的hamcrest,则可能会出现此错误。例如,将junit dep与hamcrest 1.3一起使用需要使用来自hamcrest的assertThat而不是junit

因此,解决办法是使用

import static org.hamcrest.matcherasert.assertThat

而不是


import static org.junit.Assert.assertThat

我正在比较多个项目。(hasItems,而不是hasItem)那么,hamcrest根本不支持比较两个集合?(我没有实际的物品清单,我正在建立一个从0到999的数字清单,我必须比较收藏)。我不能为你回答这个问题,我是根据文档来告诉你的。但是有一种更简单的方法来做你想做的事。。。使用assertTrue(actual.containsAll(expected))。这更简单,但是当这个断言失败时,您会得到一条非常隐秘的错误消息,它没有我想象的hasItems输出的错误消息有用。哇,有趣。你是说上面的代码是合法的java代码吗?不,我是说javac有时会拒绝合法的java代码,hamcrest是一个常见的原因。我没有测试过这个答案,因为我很久以前就丢失了这个源代码(移动了公司两次),但我给了你一个大拇指,让你重新使用它。好提示。似乎是将assertThat()用于任何集合匹配器的唯一“直接”方法,因为Hamcrest将其集合匹配器泛型更改为我希望的
hasItems
也匹配顺序。这似乎不是,这是错误的。此答案检查
actual
是否包含列表
expected
,而不是
actual
是否包含列表
expected
的元素。使用
进行尝试。改为添加(1)
:此代码永远不会通过。编译器错误有一个很好的原因。用石膏来压制它不是一个好主意。Yeees,考虑到我继续并通过了考试,至少可以说是令人困惑的。正确答案是:ArrayList actual=new ArrayList();应为ArrayList=新的ArrayList();增加(1);增加(2);assertThat(实际,hasItems(预期的.toArray(新整数[expected.size()]));
hasItems(1, 2);
ArrayList<Integer> actual = new ArrayList<Integer>();
ArrayList<Integer> expected = new ArrayList<Integer>();
actual.add(1);
expected.add(2);
assertThat(actual, (Matcher) hasItems(expected));
    ArrayList<Integer> actual = new ArrayList<Integer>();
    ArrayList<Integer> expected = new ArrayList<Integer>();
    actual.add(1);
    expected.add(2);
    assertThat(actual, hasItems(expected.toArray(new Integer[expected.size()])));