hamcrest tests always fail

JavaJunitHamcrest

Java Problem Overview


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)

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Solution 3 - Java

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.

Solution 4 - Java

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.

Solution 5 - Java

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

Solution 6 - Java

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.

Solution 7 - Java

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

Solution 8 - Java

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.

Solution 9 - Java

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.

Solution 10 - Java

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 : http://i.stack.imgur.com/Y5R15.png

Solution 11 - Java

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

Solution 12 - Java

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

Solution 13 - Java

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.

Solution 14 - Java

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

Solution 15 - Java

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.

Solution 16 - Java

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.

Solution 17 - Java

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.

Solution 18 - Java

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.

Solution 19 - Java

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)

Solution 20 - Java

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

Solution 21 - Java

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.

Solution 22 - Java

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

Solution 23 - Java

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionbeachw08View Question on Stackoverflow
Solution 1 - Javabeachw08View Answer on Stackoverflow
Solution 2 - JavaHunsuView Answer on Stackoverflow
Solution 3 - JavaRade_303View Answer on Stackoverflow
Solution 4 - JavaAvinav KView Answer on Stackoverflow
Solution 5 - JavaKkkevView Answer on Stackoverflow
Solution 6 - JavaimbondView Answer on Stackoverflow
Solution 7 - JavaJannemanView Answer on Stackoverflow
Solution 8 - JavaPRITEN PATELView Answer on Stackoverflow
Solution 9 - JavaJohn MarkView Answer on Stackoverflow
Solution 10 - JavaVaibhav GuptaView Answer on Stackoverflow
Solution 11 - JavaYogesh SamantView Answer on Stackoverflow
Solution 12 - JavaG.MView Answer on Stackoverflow
Solution 13 - JavaJlearnerView Answer on Stackoverflow
Solution 14 - JavaPipoView Answer on Stackoverflow
Solution 15 - JavaRichard BView Answer on Stackoverflow
Solution 16 - JavaRavi NAGALINGAMView Answer on Stackoverflow
Solution 17 - Javauser6629913View Answer on Stackoverflow
Solution 18 - Javajet.lauView Answer on Stackoverflow
Solution 19 - JavaChetan GoleView Answer on Stackoverflow
Solution 20 - Javajbunton10View Answer on Stackoverflow
Solution 21 - Javauser666View Answer on Stackoverflow
Solution 22 - JavaPTTView Answer on Stackoverflow
Solution 23 - JavaAhmed SalahView Answer on Stackoverflow