bgarrels / hamcrest

Automatically exported from code.google.com/p/hamcrest
0 stars 0 forks source link

Cannot use latest hamcrest version with JUnit 4.10 #166

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I would like to use the latest hamcrest version along with JUnit 4.10 in my 
Maven project. It works with "hamcrest-all-1.1"

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit-dep</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-all</artifactId>
      <version>1.1</version>
      <scope>test</scope>
    </dependency>

but not with the latest 1.3.RC2

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit-dep</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-integration</artifactId>
      <version>1.3.RC2</version>
      <scope>test</scope>
    </dependency>

When running a unit test that uses 
org.hamcrest.Matchers.lessThanOrEqualTo(...), I get the following exception:

java.lang.NoClassDefFoundError: org/hamcrest/TypeSafeMatcher
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at org.hamcrest.Matchers.lessThanOrEqualTo(Matchers.java:331)
    [8-< snip 8-<]
Caused by: java.lang.ClassNotFoundException: org.hamcrest.TypeSafeMatcher
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 38 more

Original issue reported on code.google.com by mburger313@gmail.com on 24 Nov 2011 at 2:18

GoogleCodeExporter commented 9 years ago
Place your hamcrest dependency above JUnit

    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-integration</artifactId>
      <version>1.3.RC2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit-dep</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>

This is because JUnit 4.10 includes an older hamcrest package.

Original comment by oliver....@gmail.com on 28 Nov 2011 at 7:44

GoogleCodeExporter commented 9 years ago
I just saw in the Maven Dependency Hierarchy view in Eclipse that hamcrest-core 
1.3.RC2 was omitted for conflict with 1.1. Indeed, junit-dep pulls 
hamcrest-core 1.1.

Thus,

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit-dep</artifactId><!-- junit without hamcrest -->
      <exclusions>
        <exclusion>
          <groupId>org.hamcrest</groupId>
          <artifactId>hamcrest-core</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

does the trick.

However, I don't get the point of providing an artifact like "junit-dep" that 
again depends on the left out classes - see 
https://github.com/KentBeck/junit/issues/349.

Original comment by mburger313@gmail.com on 28 Nov 2011 at 9:06

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Better is to use Dependency Management to solve this.

You can put this into your parent pom or (if you are not using one) in your 
project pom:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-core</artifactId>
      <version>1.3.RC2</version>
    </dependency>
  </dependencies>
</dependencyManagement>

This is not really a hamcrest issue. It is the regular maven dependency 
resolution mechanism...

Closing this issue because for hamcrest here is nothing to do.

Original comment by marc.von...@gmail.com on 12 Apr 2012 at 7:31