89

JUnit4.8がHamcrestマッチャーとどのように連携するか理解できません。junit-4.8.jarの中にいくつかのマッチャーが定義されていorg.hamcrest.CoreMatchersます。同時に、には他のマッチャーhamcrest-all-1.1.jarがいくつかありorg.hamcrest.Matchersます。それで、どこに行くのですか?プロジェクトにhamcrestJARを明示的に含め、JUnitによって提供されるマッチャーを無視しますか?

特に、私はempty()マッチャーに興味があり、これらの瓶のいずれにもそれを見つけることができません。他に何か必要ですか?:)

そして哲学的な質問:なぜJUnitはorg.hamcrest、オリジナルのhamcrestライブラリを使用するように勧めるのではなく、パッケージを独自のディストリビューションに含めたのでしょうか。

4

8 に答える 8

51

バージョンが1.2以上のHamcrestを使用している場合は、を使用する必要がありますjunit-dep.jar。このjarにはHamcrestクラスがないため、クラスのロードの問題を回避できます。

JUnit 4.11以降、junit.jarそれ自体にはHamcrestクラスがありません。もう必要はありjunit-dep.jarません。

于 2011-09-08T19:42:18.190 に答える
50

junitは、Matchersを使用するassertThat()という名前の新しいチェックassertメソッドを提供し、より読みやすいテストコードとより優れた失敗メッセージを提供する必要があります。

これを使用するために、junitに含まれているいくつかのコアマッチャーがあります。基本的なテストのためにこれらから始めることができます。

より多くのマッチャーを使用したい場合は、自分で作成するか、hamcrestlibを使用できます。

次の例は、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 fluentassertionsAPIをぜひお試しください。Hamcrestと競合していますが、静的インポートが1つだけ必要な、はるかに簡単なAPIを備えています。FESTを使用してcpaterによって提供されるコードは次のとおりです。

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は、元のhamcrestライブラリを使用するように勧めるのではなく、org.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

JUnit-4.12とJUnit-Dep-4.10の両方に、それぞれの.xmlファイルに応じたHamcrestの依存関係があります。

さらに調査すると、依存関係は.xmlファイルで作成されましたが、ソースとクラスはjarファイルであることがわかりました。build.gradleの依存関係を除外する方法のようです...すべてをクリーンに保つためにテストします。

ただのfyi

于 2015-09-12T00:20:29.997 に答える