8

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
  • 615
  • 3
  • 11
  • 23

2 Answers2

8

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.

Alex
  • 2,953
  • 1
  • 27
  • 37
  • Do 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. – user1772643 Mar 27 '13 at 14:27
  • Having 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) – ben75 Mar 27 '13 at 15:21
  • @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. – Alex Mar 27 '13 at 17:45
  • With JUnit 4.11, when I remove the dependency for `hamcrest-all` from my `pom.xml`, I get compile errors in my tests. I assume `hamcrest-core` contains only the basic classes required to interact with Hamcrest including `Matcher`, `MatcherAssert` and `AssertionError`. You'll need the other JARs to get usable matchers. – David Harkness Mar 28 '13 at 01:05
  • @Alex I'd expect the scope to be 'test', is this not the case? – tkane2000 Dec 10 '14 at 17:29
  • Yes sorry, should be test – Alex Dec 11 '14 at 08:59
6

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...

Dan
  • 1,955
  • 17
  • 21