How to use JUnit and Hamcrest together?

I can't figure out how JUnit 4.8 should work with Hamcrest combinators. There are some patterns in junit-4.8.jar defined inside junit-4.8.jar . At the same time, there are a few more matches in hamcrest-all-1.1.jar in hamcrest-all-1.1.jar . So where to go? Should I explicitly include the hamcrest JAR file in the project and ignore the matches provided by JUnit?

In particular, I am interested in the empty() match and cannot find it in any of these jars. Do I need something else? :)

And a philosophical question: why did JUnit include the org.hamcrest package in its own distribution instead of prompting us to use the original hamcrest library?

+79
java junit hamcrest
Apr 6 2018-11-11T00:
source share
7 answers

junit provides new assert validation methods called assertThat (), which Matchers uses, and should provide more readable test code and better error messages.

To use this, junit has some basic connectors. You can start with them for basic tests.

If you want to use more helpers, you can write them yourself or use lib.bmp.

The following example shows how to use empty matches in 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 hamcrest-all.jar in my build path)

+46
Apr 6 2018-11-11T00:
source share

If you are using Hamcrest with a version greater than or equal to 1.2, you should use junit-dep.jar . There are no Hamcrest classes in this bank, and therefore you avoid loading problems.

Since JUnit 4.11 junit.jar itself junit.jar not have Hamcrest classes. No longer need junit-dep.jar .

+47
Sep 08 2018-11-11T00:
source share

Answering your question directly, but you should definitely try FEST-Assert to freely approve the API. It competes with Hamcrest, but has a much simpler API that requires only one static import. 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> 
+24
Apr 06 '11 at 16:59
source share

Also, if JUnit 4.1.1 + Hamcrest 1.3 + Mockito 1.9.5 is used, make sure mockito-all is not used. It contains the main classes of Hamcrest. Use mockito-core instead. The following is the configuration:

 <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> 
+16
Jan 13 '14 at 17:48
source share

Since the versions are changing all the time, I publish to let people know that from December 2, 2014, the instructions are 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, although these are exactly:

 <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> 
+4
Dec 02 '14 at 17:47
source share

why did JUnit include the org.hamcrest package in its own distribution instead of prompting us to use the original hamcrest library?

I would suggest that since they wanted assertThat be part of JUnit. Thus, the Assert class must import the org.hamcrest.Matcher interface, and it cannot do this if JUnit is not dependent on Hamcrest or does not include (at least part) Hamcrest. And I guess that part of it was simpler, so that JUnit could be used without any dependencies.

+3
Jun 30 '11 at 0:19
source share

Both JUnit-4.12 and JUnit-Dep-4.10 have Hamcrest dependencies according to the corresponding .xml files.

Further research shows that although the dependency was made in .xml files, source and classes in banks. This seems to be a way to eliminate dependency in build.gradle ... check it out so that everything is clean.

Just fyi

0
Sep 12 '15 at 0:20
source share



All Articles