如何一起使用 JUnit 和 Hamcrest?

发布于 2024-10-30 18:54:59 字数 658 浏览 2 评论 0原文

我不明白 JUnit 4.8 应该如何与 Hamcrest 匹配器一起工作。 junit-4.8.jar< 中定义了一些匹配器/code>org.hamcrest.CoreMatchers 中。同时,hamcrest-all-1.1.jarorg.hamcrest.Matchers 中。那么,该去哪里呢?我是否应该明确地将 hamcrest JAR 包含到项目中并忽略 JUnit 提供的匹配器?

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

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

I can't understand how JUnit 4.8 should work with Hamcrest matchers. There are some matchers defined inside junit-4.8.jar in org.hamcrest.CoreMatchers. At the same time there are some other matchers in hamcrest-all-1.1.jar in org.hamcrest.Matchers. So, where to go? Shall I explicitly include hamcrest JAR into the project and ignore matchers provided by JUnit?

In particular, I'm interested in empty() matcher and can't find it in any of these jars. I need something else? :)

And a philosophical question: why JUnit included org.hamcrest package into its own distribution instead of encouraging us to use original hamcrest library?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

花之痕靓丽 2024-11-06 18:54:59

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

从 JUnit 4.11 开始,junit.jar 本身没有 Hamcrest 类。不再需要junit-dep.jar

If you're using a Hamcrest with a version greater or equal than 1.2, then you should use the junit-dep.jar. This jar has no Hamcrest classes and therefore you avoid classloading problems.

Since JUnit 4.11 the junit.jar itself has no Hamcrest classes. There is no need for junit-dep.jar anymore.

眼中杀气 2024-11-06 18:54:59

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

为了使用它,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)

junit provides new check assert methods named assertThat() which uses Matchers and should provide a more readable testcode and better failure messages.

To use this there are some core matchers included in junit. You can start with these for basic tests.

If you want to use more matchers you can write them by yourself or use the hamcrest lib.

The following example demonstrates how to use the empty matcher on an 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()));

    }
}

(I included the hamcrest-all.jar in my buildpath)

凉栀 2024-11-06 18:54:59

不完全回答您的问题,但您绝对应该尝试 FEST-Assert 流畅的断言 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>

Not exactly answering your question, but you should definitely try FEST-Assert fluent assertions API. It's competing with Hamcrest, but has a much easier API with only one static import required. Here is the code provided by cpater using 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();
    }  
}

EDIT: Maven coordinates:

<dependency>
  <groupId>org.easytesting</groupId>
  <artifactId>fest-assert</artifactId>
  <version>1.4</version>
  <scope>test</scope>
</dependency>
蓝眸 2024-11-06 18:54:59

另外,如果使用 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>

Also, if JUnit 4.1.1 + Hamcrest 1.3 + Mockito 1.9.5 are being used, make sure mockito-all is not used. It contains Hamcrest core classes. Use mockito-core instead.
The below config works :

<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>
℉服软 2024-11-06 18:54:59

由于版本一直在变化,我发帖是为了让人们知道,自 2014 年 12 月 2 日起,http://www.javacodegeeks.com/2014/03/how-to-test-dependency- 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>

Since versions are changing all the time, I'm posting to let people know that as of December 2, 2014, the instructions at http://www.javacodegeeks.com/2014/03/how-to-test-dependencies-in-a-maven-project-junit-mockito-hamcrest-assertj.html worked for me. I did not use AssertJ though, just these:

<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>
二智少女 2024-11-06 18:54:59

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

我猜这是因为他们希望 assertThat 成为 JUnit 的一部分。因此,这意味着 Assert 类必须导入 org.hamcrest.Matcher 接口,并且除非 JUnit 依赖于 Hamcrest,或者包含(至少部分的)汉克雷斯特。我想包含其中的一部分会更容易,这样 JUnit 就可以在没有任何依赖的情况下使用。

why JUnit included org.hamcrest package into its own distribution instead of encouraging us to use original hamcrest library?

I would guess that's because they wanted the assertThat to be part of JUnit. So that means the Assert class has to import the org.hamcrest.Matcher interface and it can't do that unless JUnit either depends on Hamcrest, or includes (at least part of) Hamcrest. And I guess including part of it was easier, so that JUnit would be usable without any dependencies.

不如归去 2024-11-06 18:54:59

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")
}

In 2018 using most modern libraries:

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")
}
蓝戈者 2024-11-06 18:54:59

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

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

仅供参考

Both JUnit-4.12 and JUnit-Dep-4.10 has Hamcrest dependencies according to the respective .xml files.

Further investigation shows that although the dependency was made in the .xml files, the source and classes in the jars. The seems to be a way of excluding the dependency in build.gradle ... testing it out to keep everything clean.

Just an f.y.i.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文