hamcrest tests always fail
Asked Answered
F

24

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)
Foxtail answered 11/3, 2012 at 1:58 Comment(6)
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?)Tatary
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. :)Derisible
Could you post the error's stacktrace.Coulter
I am using 1.3.0RC2. Thanks MatrixFrog I didn't even think of that.Foxtail
Is it possible that this issue still exists for Maven users?!Wassail
@DusanVasiljevic I solded the problem including hamcrest library in the pom and removing the JUnit Library (of Eclipse) from the Build Path.Infarction
F
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.

Foxtail answered 11/3, 2012 at 23:5 Comment(8)
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.Hako
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?Him
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).Maegan
had a same problem. Your solution worked for me. Thanx!Rosenarosenbaum
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.Collette
This didn't help me. I am using rest-assured 3.0.3 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.Listed
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)"Facture
@Him 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.Sequential
I
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.

Implosive answered 5/4, 2015 at 9:4 Comment(2)
This helped me. For some reason even when ellipse's junit4 was under the maven library it didn't work.Portuna
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.Lardaceous
T
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.

Thole answered 28/1, 2013 at 16:47 Comment(2)
An easy mistake to make in Eclipse with Maven projects.Dichromate
Yes, removing Junit 4 from my build path solves this issue.Lyell
I
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.

Isooctane answered 27/9, 2017 at 10:12 Comment(2)
This worked for me. The error was because the ArrayList sign I used (the java.util one) didn't match.Sloe
This worked for me too. It is simple, concise and does not mess with core JARs.Cornejo
V
6

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

Vern answered 13/3, 2012 at 17:41 Comment(1)
Since junit 4.11, junit.jar does no longer include the hamcrest classes. junit-dep.jar is deprecated. github.com/junit-team/junit/wiki/Use-with-MavenLeading
C
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.

Coreencorel answered 28/1, 2016 at 15:38 Comment(0)
B
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...)

Bug answered 27/10, 2017 at 5:30 Comment(0)
V
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.
Varices answered 15/6, 2019 at 21:39 Comment(1)
Using hamcrest-all instead of hamcrest-library was the solution in my case.Nell
P
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.

Preserve answered 9/1, 2015 at 21:26 Comment(0)
B
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

Belly answered 11/3, 2016 at 10:16 Comment(0)
S
2

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

Septa answered 5/7, 2019 at 15:13 Comment(0)
P
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

Pasquil answered 13/4, 2020 at 15:50 Comment(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. :)Seethrough
A
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.

Alliterative answered 9/12, 2015 at 6:43 Comment(0)
R
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

Rejuvenate answered 4/5, 2021 at 15:1 Comment(0)
W
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.

Whereby answered 27/11, 2014 at 22:57 Comment(0)
H
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.

Harbinger answered 1/4, 2016 at 9:13 Comment(0)
S
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.

Silicify answered 26/8, 2016 at 12:52 Comment(0)
L
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.

Lightweight answered 7/5, 2017 at 11:27 Comment(0)
R
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)

Riedel answered 8/5, 2017 at 11:27 Comment(0)
M
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).

Mum answered 31/5, 2017 at 18:59 Comment(0)
A
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.

Aubreir answered 19/12, 2017 at 14:21 Comment(0)
B
0

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

Bunghole answered 8/5, 2018 at 7:40 Comment(0)
M
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)

Mossman answered 11/5, 2022 at 10:19 Comment(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.Machinist
B
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.

Bookbindery answered 17/6, 2023 at 16:4 Comment(1)
This is was already answered by Avinav.Fiche

© 2022 - 2024 — McMap. All rights reserved.