junit and hamcrest declaration
Solution 1
If you browse to search.maven.org you can search for artifacts and see their dependencies. If you are using Eclipse wit the Maven plugin, you can also click Dependency Hierarchy in the POM editor.
Looking on the Maven website you can see that JUnit 4.11 depends on Hamcrest 1.3:
<dependencies>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>compile</scope>
</dependency>
</dependencies>
Hamcrest library you have to add yourself.
Solution 2
JUnit 4.10 & JUnit 4.11 (as depicted below):
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
... ship with the hamcrest-core 1.1 and 1.3 respectively. You can see this for yourself by leveraging the dependency plugin's tree goal (running mvn dependency:tree):
$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testng 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ testng ---
[INFO] testng:testng:jar:1.0-SNAPSHOT
[INFO] \- junit:junit:jar:4.10:test
[INFO] \- org.hamcrest:hamcrest-core:jar:1.1:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.483s
[INFO] Finished at: Fri Mar 29 12:07:22 MDT 2013
[INFO] Final Memory: 5M/81M
[INFO] ------------------------------------------------------------------------
As silly as this sounds, you need to include the appropriate hamcrest-library artefact to take advantage of the Hamcrest Matchers. Hopefully this helps...
user1772643
Updated on June 14, 2022Comments
-
user1772643 almost 2 years
I am using junit at 4.10 and declared hamcrest-core at 1.3 and hamcrest-library at 1.3. My question is are hamcrest-library and hamcrest-core embedded in junit 4.10. what about junit 4.11?
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</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>
-
user1772643 about 11 yearsDo I need to add hamcrest-core 1.3 dependency explicitly when I use junit 4.11?? When I googled it it said that "JUnit 4.11 no longer includes the org.hamcrest classes." both statements are contradicting.
-
ben75 about 11 yearsHaving hamcrest as a dependency and including hamcrest classes are 2 distinct things. "including classes" means that the classes are in the junit.jar. "as a dependency" means that you need the hamcrest jar to build/run junit. (if you are using maven: dependency will auto-magically included in your classpath. If you are not using maven you have to add the dependency manually in your classpath)
-
Alex about 11 years@ben75 is right. They mean there are no more Hamcrest classes in the jar. However it depends on them being in another jar, which it depends on.
-
David Harkness about 11 yearsWith JUnit 4.11, when I remove the dependency for
hamcrest-all
from mypom.xml
, I get compile errors in my tests. I assumehamcrest-core
contains only the basic classes required to interact with Hamcrest includingMatcher
,MatcherAssert
andAssertionError
. You'll need the other JARs to get usable matchers. -
tkane2000 over 9 years@Alex I'd expect the scope to be 'test', is this not the case?
-
Alex over 9 yearsYes sorry, should be test