52

I am using hamcrest 1.3 to test my code. It is simply a die. I am trying to test it to make sure the number generated is less than 13. I had a print statement that printed what the number generated was. The number generated was always less than 13 but the test always failed. Is there something I am doing wrong?

This is the code I am testing.

import java.util.Random;

public class Die {
    private int numSides;
    Random rand;

    public Die(int numSides){
        this.numSides = numSides;
        rand = new Random(System.currentTimeMillis());
    }

    public int roll(){
        return rand.nextInt(numSides) + 1;
    }
}

And this is my test code.

import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;

public class DieTest {
    @Test
    public void testRoll() {
        Die x = new Die(12);    
        assertThat(x.roll(), is(lessThan(13)));
    }
}

Edit: This is the failure stack trace.

java.lang.SecurityException: class "org.hamcrest.Matchers"'s signer information does not match signer information of other classes in the same package
at java.lang.ClassLoader.checkCerts(Unknown Source)
at java.lang.ClassLoader.preDefineClass(Unknown Source)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at DieTest.testRoll(DieTest.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
beachw08
  • 1,427
  • 1
  • 10
  • 8
  • Works fine for me. Note that this tests only a single roll, obviously not overly-helpful. (Slightly take that back; if I change the value to `8` and get a higher roll, I get a Hamcrest error, using 1.3.0RC2. What version are you using?) – Dave Newton Mar 11 '12 at 02:58
  • As an aside, your code will produce every number from 1 to 12 with equal probability. But if you roll two standard dice, different numbers come up with different probabilities (7 being most likely, 2 and 12 being least likely, and 1 being impossible). So in case that's what you're trying to model, your code is not accurate. :) – Tyler Mar 11 '12 at 11:13
  • Could you post the error's stacktrace. – Stefan Birkner Mar 11 '12 at 14:03
  • 1
    I am using 1.3.0RC2. Thanks MatrixFrog I didn't even think of that. – beachw08 Mar 11 '12 at 16:51
  • Is it possible that this issue still exists for Maven users?! – Hedrack Apr 03 '16 at 23:32
  • @DusanVasiljevic I solded the problem including hamcrest library in the pom and removing the JUnit Library (of Eclipse) from the Build Path. – zer0uno May 16 '16 at 13:52

24 Answers24

78

This is the site that help me solve the problem.

http://code.google.com/p/hamcrest/issues/detail?id=128

The hamcrest.jar needs to go before the Junit library in the build path.

beachw08
  • 1,427
  • 1
  • 10
  • 8
  • 6
    Important to remember (that is not quite clear at first) that hamcrest (or jmock) must be before junit in `Order and Export` tab, not `Libraries`. – Schultz9999 Aug 04 '13 at 23:23
  • 12
    I'm using Maven; even though I list the hamcrest jars before JUnit in my pom.xml, Eclispe still seems to use its internal Junit/Hamcrest jars, so I still get this 'signer information does not match' error. Running 'mvn test' outside of Eclipse runs without error. The above link does not mention Maven. Any tips or help to get Eclipse JUnit runner to work? – djb Oct 04 '13 at 18:11
  • 2
    I'm in the Same boat as dbj (the above thread). Our project uses Eclipse and Maven, and I've been unable to solve this issue, for the reasons outlined above. I spent hours searching for a solution, and never found one. However, in my quest for a better assertion mechanism, I found FEST-ASSERT. I think it's better than Hamcrest, so my problem is now solved (though not in the way I originally intended). – Robert Patterson Oct 25 '14 at 17:02
  • had a same problem. Your solution worked for me. Thanx! – Invy Oct 31 '14 at 18:12
  • Make sure you use hamcrest-all.jar and not only hamcrest-library.jar otherwise the matchers in the library won't be compatible with the core matchers that ship with Junit and you will still have the security exception even if the hamcrest library comes first in your build path. – Gael Nov 18 '14 at 08:16
  • 1
    This didn't help me. I am using [rest-assured 3.0.3](https://github.com/rest-assured/rest-assured) and importing eclipses JUnit 4 will result in the aforementioned error. To fix this I added a further dependency to my `pom.xml`, namely JUnit (4.4) itself and instead of `Run As - JUnit test` I now run as `Maven test` - which works perfectly. – Timothy Dalton May 18 '17 at 07:01
  • 2
    From the link for the lazy: "Easy Fix: replace $ECLIPSE_HOME\plugins\org.hamcrest.core_1.3.0.v201303031735.jar with Maven hamcrest-core-1.3.jar (obviously renaming it to same name as eclipse jar)" – lmo Jan 11 '18 at 08:40
  • 1
    @djb nailed the problem description, after a bit I got it working. Eclipse Oxygen failed with the ugly signer error but Maven was succeeding. Since Maven 2.something classpath ordering in the POM is defined by top-to-bottom ordering of dependencies. Even though the Hamcrest dep was last in the POM (JUnit 4.12 above it), Maven was still running tests fine. In Eclipse build path settings I had listed the Maven Deps first, then JUnit, then Java. So I was baffled. The JUnit tests all started working in Eclipse when I moved the Hamcrest dep in the POM to the top. – dan Sep 20 '18 at 13:01
19

I just removed JUnit library from my project configuration. I still can run the tests as JUnit is also included in my pom file. So the solution just use the library from Maven.

Hunsu
  • 3,281
  • 7
  • 29
  • 64
  • This helped me. For some reason even when ellipse's junit4 was under the maven library it didn't work. –  Aug 25 '16 at 11:38
  • This is what fixed the issue for me. In the project build path, I had JUnit pulled in both as a separate library and as a jar under Maven dependencies. Once I removed the library, the problem went away. – Kate Barnett Nov 15 '18 at 21:02
14

In my Eclipse inside Project settings in Java Build Path section, Libraries I have previously added internal JUnit library which uses JUnit version 4.8 and hamcrest-core version 1.1. I believe that that was causing this error in my case.

I leave this bit of information here, maybe somebody else would benefit from my experience.

Rade_303
  • 905
  • 11
  • 28
11

If you are using a Maven project, simply remove the Junit library from the build path and instead import Junit and Hamcrest separately via POM.

Avinav K
  • 111
  • 1
  • 3
6

Use junit-dep.jar rather than junit.jar- this is JUnit minus its dependencies. Junit.jar contains an old version of Hamcrest.

Kkkev
  • 4,716
  • 5
  • 27
  • 43
  • 3
    Since junit 4.11, junit.jar does no longer include the hamcrest classes. junit-dep.jar is deprecated. https://github.com/junit-team/junit/wiki/Use-with-Maven – leo Jan 14 '16 at 15:39
4

First of all make sure that you have added JUnit dependency in POM.xml file.

Now, right click on the project and go to properties, select Java build path and select Libraries tab.

In my case there were Maven dependencies, JRE and Junit4 libraries. And I just removed Junit library and it works for me. Or one can also reorder the libraries as due to build order of Hamcrest and JUnit4 the problem was occurring.

imbond
  • 2,030
  • 1
  • 20
  • 22
4

Johan Mark (above) suggested to

rename the file $ECLIPSE_HOME\plugins\org.hamcrest.core_1.3.0.v201303031735.jar to something like *.bak or remove the file."

Renaming/removing the file caused my Eclipse Junit library to stop working, but replacing the JAR file with a copy of the same version from my Maven repo made the certificate problem go away.

(As someone on Google remarked, the Eclipse Junit copy of hamcrest has a cert issue but the Maven copy does not...)

cramopy
  • 3,459
  • 6
  • 28
  • 42
Janneman
  • 59
  • 1
  • 2
3

If you are Using Maven:

Steps:

  1. Add Latest Hamcrest Dependency into POM, from here https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all

  2. Add Latest JUnit Dependency into POM,from Here https://mvnrepository.com/artifact/junit/junit

  3. Remove Any JUnit libraries from the Build path.

As Shown here

and Here

  1. After you have Completed all above steps , Refresh your Project and Run.
2

I was getting the same exception. Like beachw08 recommended, I referred to:

http://code.google.com/p/hamcrest/issues/detail?id=128

One of the posts said:

rename the file $ECLIPSE_HOME\plugins\org.hamcrest.core_1.3.0.v201303031735.jar to something like *.bak or remove the file.

I did this and it solved my problem.

John Mark
  • 2,458
  • 2
  • 21
  • 17
2

If you get the following exception "java.lang.SecurityException: class "org.hamcrest.Matchers"'s signer information does not match signer information of other classes in the same package", ensure that the hamcrest jar is before the Junit library in the build path. You can configure the order in the project properties under Java Build Path on the Order and Export tab. click below image link for more clarity : https://i.stack.imgur.com/Y5R15.png

Vaibhav Gupta
  • 1,035
  • 1
  • 8
  • 16
2

Removed the JUNIT 4 library from the libraries tab on Eclipse -> Java Build path and it worked.

cs95
  • 379,657
  • 97
  • 704
  • 746
2

I had the same problem. Right Click on the project / Order and Export/ move up your hamcrest lib to the first position, for some reason it has to go first than your Junit lib

G.M
  • 21
  • 1
  • Heya, your answer that's really specific to the IDE that's being used. It'd be useful to explain what you're doing in more detail (eg changing dependency order by...) so that anyone not using the same IDE as you can still benefit from your answer. :) – saywhatnow Apr 13 '20 at 20:14
1

I resolved this problem by removing Junit4 library from the Build Path and added TestNG Library to the build path and imported TestNG annotations instead of Junit4 annotations in my java program.

Jlearner
  • 573
  • 4
  • 6
1

With eclipse, I had the same issue but with mvn commandline was working. Solved it by removing Junit into the build path, not in order and export. Above example is before removing it. enter image description here

Pipo
  • 4,653
  • 38
  • 47
0

I had the same problem as detailed here. I believe the problem comes down to the junit4 jar file.

If, under eclipse pom editor, you look at the junit4 Hierarchy you will see that it has a dependency on hamcrest-core (i.e. hamcrest-core will, by default, be pulled in on compile). In my unit test code I use the hamcrest collection Matchers (org.hamcrest.collection). These aren't included in the core jar and I set up a dependency on hamcrest-all in the pom. Doing this duplicates the hamcrest-core inclusion and appear to leave you open to a version mismatch with the junit hamcrest-core dependency and hence the security exception. I removed the hamcrest-all dependency and replaced it with hamcrest-library and the exception went away.

If you only use core hamcrest then you should not set up your own dependency and rely on the version junit pulls in. Alternately, as suggested in another comment, use junit-dep to strip out the junit dependency and then include hamcrest-all.

Richard B
  • 895
  • 13
  • 39
0

i recently had this problem with eclipse and Junit.

To solve this, i did that:

1 - Download the latest hamcrest-all jar from here : https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/hamcrest/

2- Go on the eclipse installation folder: eclipse/plugin/ and find the org.hamcrest...jar

3- make a backup of the step 2 jar and replace it by the step 1 jar (rename it same as the jar step 2).

4- restart eclipse

After that, my issue was solved.

Ravi NAGALINGAM
  • 13
  • 1
  • 1
  • 6
0

When trying to solve this problem for your particular context, keep in mind the stack trace above is merely a symptom. The solutions may work for some people, but not others.

For instance:

  • Putting the Hamcrest JAR before the JUnit JAR in the classpath will work in situations where the version of JUnit in use (older) contains Hamcrest classes
  • Overlaying the version of Hamcrest used internally by Eclipse with a 'stock' version that's been renamed to match the internal version may work if no other Eclipse plugin bundle uses manifest information in the original internal JAR

In my case, the symptom above was caused by a Hamcrest JAR used internally and provided by Eclipse, and when I tried to replaced it with a 'stock' renamed version, anything related to JUnit failed to load when I started Eclipse. After I reverted back to the original internal version, the SecurityException returned. The solution that worked for me was to delete the manifest in the JAR using 7-Zip. This effectively 'unsigned' the JAR and now my particular configuration works.

user6629913
  • 180
  • 1
  • 14
0

my environment Mac OS + eclipse, I found org.hamcrest.core_1.3.0.v201303031735.jar is in my JUnit 4, so I can't make it forward than junit.jar.

so I delete it from path ~/.p2/pool/plugins/, then refresh project, it works.

jet.lau
  • 21
  • 4
0

This one resolved my issue :

Replace $ECLIPSE_HOME\plugins\org.hamcrest.core_1.3.0.v201303031735.jar with Maven or your project's lib's hamcrest-core-xx.jar (obviously renaming it to same name as eclipse jar)

Chetan Gole
  • 703
  • 2
  • 14
  • 26
0

I went into the build properties for the project and changed JUNIT from version 4 to version 3 and it works fine now.

Interestingly I still have version 4 in my pom.xml so I am inclined to think that this is an eclipse issue (I was able to build and run my tests via terminal just fine).

jbunton10
  • 60
  • 5
0

I did the following:

First in pom file i excluded hamcrest-core from junit dependency and used instead hamcrest-all. Second i removed from build path the eclipse JUNIT as it overrides the maven one. The ordering didn't affect my jars since the bad jar was excluded.

user666
  • 1,750
  • 3
  • 18
  • 34
0

I had exactly the same issue. I created a new project and it resolved my issue.

PTT
  • 526
  • 7
  • 27
0

just go to the project then click on build path and click on configure build path click on libraries check junit and remove it(note that junit and hamcrest in pom file)

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 12 '22 at 06:59
0

Yes, if you are using a Maven project, simply remove the Junit library from the build path and instead import Junit and Hamcrest separately via POM.