[ACCEPTED]-How to use JUnit and Hamcrest together?-hamcrest

Accepted answer
Score: 53

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

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

Score: 51

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

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

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

The following example demonstrates how 2 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 1 included the hamcrest-all.jar in my buildpath)

Score: 25

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

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

<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>
Score: 4

Since versions are changing all the time, I'm 4 posting to let people know that as of December 3 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 2 me. I did not use AssertJ though, just 1 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>
Score: 3

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

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

Score: 2

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

0

Score: 0

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

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

Just an 1 f.y.i.

More Related questions