89

我不明白 JUnit 4.8 应该如何与 Hamcrest 匹配器一起使用。junit-4.8.jar里面定义了一些匹配器org.hamcrest.CoreMatchers。同时 in 中还有一些其他匹配器。那么,去哪里呢?我是否应该在项目中明确包含 hamcrest JAR 并忽略 JUnit 提供的匹配器?hamcrest-all-1.1.jarorg.hamcrest.Matchers

特别是,我对empty()matcher 很感兴趣,但在这些 jar 中都找不到它。我需要别的东西吗?:)

还有一个哲学问题:为什么 JUnit 将org.hamcrest包包含在自己的发行版中,而不是鼓励我们使用原始的 hamcrest 库?

4

8 回答 8

51

如果您使用的 Hamcrest 版本大于或等于 1.2,那么您应该使用junit-dep.jar. 这个 jar 没有 Hamcrest 类,因此可以避免类加载问题。

从 JUnit 4.11 开始,junit.jar它本身没有 Hamcrest 类。没有必要junit-dep.jar了。

于 2011-09-08T19:42:18.190 回答
50

junit 提供了名为 assertThat() 的新检查断言方法,它使用 Matchers 并且应该提供更易读的测试代码和更好的失败消息。

为了使用它,junit 中包含了一些核心匹配器。您可以从这些开始进行基本测试。

如果您想使用更多匹配器,您可以自己编写它们或使用 hamcrest 库。

以下示例演示如何在 ArrayList 上使用空匹配器:

package com.test;

import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

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

import org.junit.Test;

public class EmptyTest {
    @Test
    public void testIsEmpty() {
        List myList = new ArrayList();
        assertThat(myList, is(empty()));

    }
}

(我在构建路径中包含了 hamcrest-all.jar)

于 2011-04-06T16:29:55.117 回答
25

不完全回答您的问题,但您绝对应该尝试FEST-Assert fluent assertions API。它与 Hamcrest 竞争,但 API 更简单,只需要一个静态导入。这是cpater使用 FEST 提供的代码:

package com.test;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import static org.fest.assertions.Assertions.assertThat;

public class EmptyTest {
    @Test
    public void testIsEmpty() {
        List myList = new ArrayList();
        assertThat(myList).isEmpty();
    }  
}

编辑:Maven坐标:

<dependency>
  <groupId>org.easytesting</groupId>
  <artifactId>fest-assert</artifactId>
  <version>1.4</version>
  <scope>test</scope>
</dependency>
于 2011-04-06T16:59:19.013 回答
19

此外,如果正在使用 JUnit 4.1.1 + Hamcrest 1.3 + Mockito 1.9.5,请确保不使用 mockito-all。它包含 Hamcrest 核心类。请改用 mockito-core。以下配置有效:

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-all</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>1.9.5</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <artifactId>hamcrest-core</artifactId>
            <groupId>org.hamcrest</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.1.1</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <artifactId>hamcrest-core</artifactId>
            <groupId>org.hamcrest</groupId>
        </exclusion>
    </exclusions>
</dependency>
于 2014-01-13T17:48:31.287 回答
4

由于版本一直在变化,我发帖是为了让人们知道,截至 2014 年 12 月 2 日,http://www.javacodegeeks.com/2014/03/how-to-test-dependencies-in上的说明-a-maven-project-junit-mockito-hamcrest-assertj.html为我工作。我没有使用 AssertJ,只是这些:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-core</artifactId>
  <version>1.9.5</version>
  <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-core</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-library</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>   
<dependency>
    <groupId>org.objenesis</groupId>
    <artifactId>objenesis</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>
于 2014-12-02T17:47:57.097 回答
3

为什么 JUnit 将 org.hamcrest 包包含在自己的发行版中,而不是鼓励我们使用原始的 hamcrest 库?

我猜那是因为他们希望assertThat成为 JUnit 的一部分。所以这意味着Assert类必须导入org.hamcrest.Matcher接口,除非 JUnit 依赖于 Hamcrest,或者包含(至少部分)Hamcrest,否则它不能这样做。而且我想包含它的一部分更容易,这样 JUnit 就可以在没有任何依赖关系的情况下使用。

于 2011-06-30T00:19:32.627 回答
2

2018 年使用最现代的图书馆:

configurations {
    all {
        testCompile.exclude group: "org.hamcrest", module: "hamcrest-core"
        testCompile.exclude group: "org.hamcrest", module: "hamcrest-library"
    }
}
dependencies {
    testCompile("junit:junit:4.12")
    // testCompile("org.hamcrest:hamcrest-library:1.3")
    // testCompile("org.hamcrest:java-hamcrest:2.0.0.0")
    testCompile("org.hamcrest:hamcrest-junit:2.0.0.0")
}
于 2018-09-29T22:49:20.860 回答
0

根据各自的 .xml 文件,JUnit-4.12 和 JUnit-Dep-4.10 都具有 Hamcrest 依赖项。

进一步的调查表明,虽然依赖关系是在 .xml 文件中建立的,但 jar 中的源代码和类。这似乎是一种排除 build.gradle 中的依赖关系的方法......对其进行测试以保持一切清洁。

仅供参考

于 2015-09-12T00:20:29.997 回答