hamcrest tests always fail - java

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)

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.

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.

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.

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.

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

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.

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

If you are Using Maven:
Steps:
Add Latest Hamcrest Dependency into POM, from here https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all
Add Latest JUnit Dependency into POM,from Here
https://mvnrepository.com/artifact/junit/junit
Remove Any JUnit libraries from the Build path.
As Shown here
and Here
After you have Completed all above steps , Refresh your Project and Run.

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.

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

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

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

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.

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.

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.

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.

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.

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.

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)

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

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.

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

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)

Related

class "org.hamcrest.Matchers"'s signer information does not match signer information of other classes in the same package [duplicate]

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)
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.
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.
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.
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.
Use junit-dep.jar rather than junit.jar- this is JUnit minus its dependencies. Junit.jar contains an old version of Hamcrest.
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.
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...)
If you are Using Maven:
Steps:
Add Latest Hamcrest Dependency into POM, from here https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all
Add Latest JUnit Dependency into POM,from Here
https://mvnrepository.com/artifact/junit/junit
Remove Any JUnit libraries from the Build path.
As Shown here
and Here
After you have Completed all above steps , Refresh your Project and Run.
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.
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
Removed the JUNIT 4 library from the libraries tab on Eclipse -> Java Build path and it worked.
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
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.
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.
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.
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.
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.
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.
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)
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).
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.
I had exactly the same issue. I created a new project and it resolved my issue.
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)

Getting ClassCastException for xerces class when importing Ant build script

(I just asked this in the Gradle forum, but I concluded that SO just works better.)
I'm looking at modifying an existing Gradle build script. It currently tries to execute "ant" in order to run a target from a specific Ant build script. This appears to be platform-specific, and it fails on my Windows box. I'm attempting to convert it to the more portable (?) process of importing the build script and executing the desired converted task.
The first thing I did was just add the "ant.importBuild" statement just before the existing target, and ran that. This fails with the following root cause:
Caused by: java.lang.ClassCastException: org.apache.xerces.parsers.XIncludeAwareParserConfiguration cannot be cast to org.apache.xerces.xni.parser.XMLParserConfiguration
at org.apache.xerces.parsers.SAXParser.<init>(Unknown Source)
at org.apache.xerces.parsers.SAXParser.<init>(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.<init>(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl.<init>(Unknown Source)
at org.apache.xerces.jaxp.SAXParserFactoryImpl.newSAXParser(Unknown Source)
at org.apache.tools.ant.util.JAXPUtils.newSAXParser(JAXPUtils.java:215)
at org.apache.tools.ant.util.JAXPUtils.getNamespaceXMLReader(JAXPUtils.java:172)
at org.apache.tools.ant.helper.ProjectHelper2.parse(ProjectHelper2.java:244)
at org.apache.tools.ant.helper.ProjectHelper2.parse(ProjectHelper2.java:177)
at org.apache.tools.ant.ProjectHelper.configureProject(ProjectHelper.java:93)
at org.apache.tools.ant.ProjectHelper$configureProject.call(Unknown Source)
at org.gradle.api.internal.project.DefaultAntBuilder.importBuild(DefaultAntBuilder.groovy:76)
From the related occurrences of this on the web, it's clear there's a classpath conflict (that didn't tell me anything that wasn't obvious), but I don't know how to resolve this.
The build scripts in question are from the Mockito code base (https://github.com/mockito/mockito), so you can see them there. I only added "ant.importBuild 'build-ant.xml'" just before the place where the Ant target is executed.
I got similar problem with xerces before which caused by a newer version xerces was used automatically due to gradle use newer one by default.
I will suggest you use gradle's dependencies task to check

Play Framework: PDF 0.9 module not playing nicely with JavaAPIforKml

My Play project currently depends on Play's pdf module version 0.9 and JavaAPIforKml 2.2.0 (used to create objects representing kml files).
Previously I was using pdf module version 0.7 and everything was working fine.
Now that I have upgraded to version 0.9, all my tests for kml based classes now fail. The exception I get is this:
java.lang.ClassNotFoundException: org.apache.xerces.jaxp.datatype.DatatypeFactoryImpl
at java.lang.ClassLoader.findClass(ClassLoader.java:358)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at
play.classloading.ApplicationClassloader.loadClass(ApplicationClassloader.java:93)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at javax.xml.datatype.FactoryFinder.getProviderClass(FactoryFinder.java:115)
at javax.xml.datatype.FactoryFinder.newInstance(FactoryFinder.java:146)
at javax.xml.datatype.FactoryFinder.findJarServiceProvider(FactoryFinder.java:298)
at javax.xml.datatype.FactoryFinder.find(FactoryFinder.java:223)
at javax.xml.datatype.DatatypeFactory.newInstance(DatatypeFactory.java:131)
at com.sun.xml.bind.DatatypeConverterImpl.<clinit>(DatatypeConverterImpl.java:833)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl$3.run(JAXBContextImpl.java:287)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl$3.run(JAXBContextImpl.java:286)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:285)
at
com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1140)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:154)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:121)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:202)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:363)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:522)
at de.micromata.opengis.kml.v_2_2_0.Kml.getJaxbContext(Kml.java:631)
at de.micromata.opengis.kml.v_2_2_0.Kml.createMarshaller(Kml.java:640)
at de.micromata.opengis.kml.v_2_2_0.Kml.marshal(Kml.java:682)
The part of the code that causes the exception is this:
de.micromata.opengis.kml.v_2_2_0.Kml kml = ...
kml.marshal(someByteArrayOutputStream); // This is the line that causes the exception
If I switch back to pdf module version 0.7, everything works again. Does anyone know what I could do to keep using pdf module version 0.9 and have the kml tests to continue to work?
In case it helps, the pdf module version 0.9 has these dependencies:
core-renderer.jar
itest-2.1.7.jar
jaxen-1.1.jar
jtidy-r938.jar
shani-parser-v1.4.17.jar
xml-apis.jar
yahp-internal.jar
yahp.jar
While the JavaApiForKml version 2.2.0 has these dependencies:
jaxb-impl 2.2
jaxb-xjc 2.2
xmlunit 1.2
I also struggled with the Play! PDF module breaking my (previously working) XML parsing.
Thanks for sharing your solution, it helped a lot! Though there is a more elegant way to achieve it.
As xerces lives on maven central, you should be able to add this to your dependencies.yml:
- xerces -> xercesImpl 2.10.0
That way you don't need to create a module for it, or worry about it being overwritten when you sync.
(I'm using 2.10.0, but you can swap that out for whichever version is most appropriate)
Cheers
This problem occurs because PDF module uses the jar file xml-apis.jar which adds org.allcolor.xml.parser.CDocumentBuilderFactory to the available DocumentBuilderFactory. Unfortunately, this document builder seems not handling very well some features, as the XPath evaluation.
When we add the Xerces library to the project, its DocumentBuilder supersedes the previous one and everything works fine. But this works by chance: it just happens that the Xerces DocumentBuilderFactory is the first one to be found.
In order to force the application to use the default DocumentBuilderFactory implementation I think that is preferable to define the corresponding system property, as defined in the javadoc.
-Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
Alternatively, we can create a new DocumentBuilderFactory instance explicitly indicating which implementation should be used:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl", null);
I've managed to get my build to work again however I don't particularly like the solution. Firstly, I download the xerces2 library from apache.
I put the jar into the lib directory and the build worked. This library never existed previously so I'm not sure how it worked before.
I then put the xerces jar in its own play "module" called xerces so that when running play deps --sync the xerces jar would not get wiped from the lib directory. After running the build using this technique the build failed again!
So after more playing around, I thought it may have something to do with the ordering of the jars. I renamed the xerces module I created to be called apache-xerces and re-ran the build and the build started working again!

java.lang.NoClassDefFoundError in junit

I am getting this error in java in my junit test code. I looked up on the net and it says that I need to add the junit.jar in the classpath.
In Eclipse I have added it in the classpath of Project Properties windows but I still get initialisation error. What should I do..?
This is the complete trace of the error:
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
at java.lang.ClassLoader.defineClass1(Native Method)
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 org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:32)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:41)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:31)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
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)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
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)
... 25 more
Right click your project in Package Explorer > click Properties
go to Java Build Path > Libraries tab
click on 'Add Library' button
select JUnit
click Next.
select in dropdown button JUnit4 or other new versions.
click finish.
Then Ok.
Eclipse -> Top menu -> Run -> Run Configurations
Delete all the occurrences of your test. Your test may appear as YourTest.Method_1(). Delete that as well.
Re-run. Let Eclipse build a fresh configuration.
Addendum: Locally I have created a "User Library" and added to my projects which has
hamcrest-core-1.3.jar
junit-4.12.jar
The same problem can occur if you have downloaded JUnit jar from the JUnit website, but forgotten to download the Hamcrest jar - both are required (the instructions say to download both, but I skipped ahead! Oops)
When using in Maven, update artifact junit:junit from e.g. 4.8.2 to 4.11.
On Eclipse I was able to solve the above issue by following the below steps :
Right-click on the test file which you want to run,
Select Run As -> Run Configurations -> Select Classpath tab -> Select to the bootstrap Entries -> Select Advanced -> Select Add library -> Select JUnit -> Next ->Select JUnit4 from the drop-down -> Finish
Then Select Apply -> Run
In my case, I added my libraries to Modulepath instead on Classpath.
It only works if JUnit is correctly added to Classpath.
The org/hamcrest/SelfDescribing class is not on the run-time classpath.
These steps worked for me when the error showed that the Filter class was missing (as reported in this false-diplicated question: JUnit: NoClassDefFoundError: org/junit/runner/manipulation/Filter ):
Make sure to have JUnit 4 referenced only once in your project (I also removed the Maven nature, but I am not sure if this step has any influence in solving the
problem).
Right click the file containing unit tests, select
Properties, and under the Run/Debug settings, remove any entries
from the Launch Configurations for that file. Hit Apply and close.
Right click the project containing unit tests, select
Properties, and under the Run/Debug settings, remove any entries
involving JUnit from the Launch Configurations. Hit Apply and close.
Clean the project, and run the test.
Thanks to these answers for giving me the hint for this solution: https://stackoverflow.com/a/34067333/5538923 and https://stackoverflow.com/a/39987979/5538923).
Try below steps:
Go to your project's run configuration. In Classpath tab add JUnit library.
Retry the same steps.
It worked for me.
I had the same issue, the problem was in the #ContextConfiguration in me test classes, i was loading the servlet context too i just change:
#ContextConfiguration(locations = { "classpath*:**\*-context.xml", "classpath*:**\*-config.xml" })
to:
#ContextConfiguration(locations = { "classpath:**\*-context.xml", "classpath:**\*-config.xml" })
and that´s it.
this way im only loading all the files with the pattern *-context.xml in me test path.
Make sure Environment variable JUNIT_HOME is set to c:\JUNIT.
then In run configuration > select classpath > add external jars junit-4.11.jar
The reason for this is "hamcrest-core" jar is not in classpath as it doesn't comes directly with junit jar. So there are two ways to resolve this:
select project -> buildpath -> add libraries and select junit (It contains both junit & hamcrest-core)
download hamcrest-core from maven repo and add this to your classpath.
Adding my two cents to other answers.
Check if you haven't by any chance created your test class under src/main/java instead of usual src/test/java. The former is the default in Eclipse when you create a new test class for whatever reason and can be overlooked. It can be as simple as that.
I was following this video: https://www.youtube.com/watch?v=WHPPQGOyy_Y
but failed to run the test. After that, I deleted all the downloaded files and add
the Junit using the step in the picture.
This error also comes if 2 versions of hamcrest-library or hamcrest-core is present in the classpath.
In the pom file, you can exclude the extra version and it works.
even Junit4.11.jar doesnot have the hamcrest-core.jar. I added explicitly in the classpath and the issue was resolved.
If you have more than one version of java, it may interfere with your program.
I suggest you download JCreator.
When you do, click configure, options, and JDK Profiles. Delete the old versions of Java from the list. Then click the play button. Your program should appear.
If it doesn't, press ctrl+alt+O and then press the play button again.

IntelliJ IDEA with Junit 4.7 "!!! JUnit version 3.8 or later expected:"

When I attempt to run the following test in IntelliJ IDEA I get the message:
"!!! JUnit version 3.8 or later expected:"
It should be noted that this is an Android project I am working on in IntelliJ IDEA 9.
public class GameScoreUtilTest {
#Test
public void testCalculateResults() throws Exception {
final Game game = new Game();
final Player player1 = new Player();
{
final PlayedHole playedHole = new PlayedHole();
playedHole.setScore(1);
game.getHoleScoreMap().put(player1, playedHole);
}
{
final PlayedHole playedHole = new PlayedHole();
playedHole.setScore(3);
game.getHoleScoreMap().put(player1, playedHole);
}
final GameResults gameResults = GameScoreUtil.calculateResults(game);
assertEquals(4, gameResults.getScore());
}
}
The full stack trace looks like this...
!!! JUnit version 3.8 or later expected:
java.lang.RuntimeException: Stub!
at junit.runner.BaseTestRunner.<init>(BaseTestRunner.java:5)
at junit.textui.TestRunner.<init>(TestRunner.java:54)
at junit.textui.TestRunner.<init>(TestRunner.java:48)
at junit.textui.TestRunner.<init>(TestRunner.java:41)
at com.intellij.rt.execution.junit.JUnitStarter.junitVersionChecks(JUnitStarter.java:152)
at com.intellij.rt.execution.junit.JUnitStarter.canWorkWithJUnitVersion(JUnitStarter.java:136)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110)
Process finished with exit code -3
This problem happens because Android Platform (android.jar) already contains JUnit classes. IDEA test runner loads these classes and sees that they are from the old JUnit, while you are trying to use annotated tests which is a feature of the new JUnit, therefore you get the error from the test runner.
The solution is simple, open the Project Structure | Modules | Dependencies, and move the junit-4.7.jar up, so that it comes before Android 1.6 Platform in the classpath. Now the test runner will be happy as it loads the new JUnit version.
my module is a java library module, so changing JRE to 1.8 java solved the issue.
Or, you can also do it globally via Module Settings > SDK Location > JDK, specifying Oracle's JDK 8 instead of Android SDK's copy.
I had this problem with a multi module project (libgdx). One module is pure Java and has tests.
My solution was to set "use alternative JRE" to "Java 1.8" in the run configuration of my unit tests. This makes sure no android.jar is on the classpath and the junit 4.x runner is used.
I got the same error when creating both Unit Test and Android Instrument Test in Android Studio 1.4+ and it started to get confused. To avoid this error make sure your test class is fall under Android Tests on Run/Debug Configurations
Make sure you follow the instruction properly https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html
Make sure Test Artifact in Build Variants is set to Android Instrumentation Tests
Click menu Run > Edit Configuration
Make sure your class/method name is inside Android Tests instead of JUnit
If it is in JUnit simply delete the config and right click on the file you want to test and Run again. It will then create the config under Android Tests section and it run on device/emulator.
For Android Studio - starting from Android Studio 1.1 Beta 4, Google has added support for Android Gradle plugin 1.1.0-RC. The new plugin supports Unit Testing through Android Studio using junit 4+.
This is still experimental and there are some manual steps to set this up.
For everyone who is reading this post and still have the same issue with AndroidStudio 1.0. You cannot change the dependency order in AndroidStudio has the IDE re-write them automatically. And, even if you manage to change the order by modifying the .iml file, you will get a "class not found...". This is because the Test output path cannot be set on AndroidStudio.
Actually, there is solution to make AndroidStudio, Junit and Robolectric working together. Take a look at this https://github.com/JCAndKSolutions/android-unit-test and use this plugin as well : https://github.com/evant/android-studio-unit-test-plugin
Works perfectly for me.
For me this problem was caused by an outdated/broken run configuration for the tests. I simply had to delete the configuration, then create a new one and the problem was fixed.
I have got the same error when i have create my own junit package
To fix this, i have added these two lines in my app gradle file as it's explained here :
dependencies {
...
// Required -- JUnit 4 framework
testCompile 'junit:junit:4.12'
// Optional -- Mockito framework
testCompile 'org.mockito:mockito-core:1.10.19'
}
I got the same message
JUnit version 3.8 or later expected
by a simple beginner's mistake. I had used the same package names and class names on src/main and src/test for a class (the HomeController class in my case):
my-test-project
+--pom.xml
+--src
+--main
+--com
+--example
+--Application.java
+--controller
+--HomeController.java
+--test
+--com
+--example
+--ApplicationTest.java
+--controller
+--HomeController.java <---- same package and class name: not good!
With that, the src/main HomeController class, as well as the src/test HomeController class, had the same full path:
com.example.controller.HomeController.class
The result: any tests that were dependent on the HomeController class have failed.
Either changing the package name and/or the class name has resolved the issue. Here the example, when both, the package name and the class name is changed:
my-test-project
+--pom.xml
+--src
+--main
+--com
+--example
+--Application.java
+--controller
+--HomeController.java
+--test
+--com
+--example
+--test <---- added (optional)
+--ApplicationTest.java
+--controller
+--HomeControllerTest.java <---- changed
Now the fully qualified class names differ. The src/main HomeController class name is:
com.example.controller.HomeController.class
and the src/test HomeHontrollerTest class name is:
com.example.test.controller.HomeControllerTest.class
With the fully qualified class names being unique, the problem disappears.
There are two thing I could imagine to happen
If your IDE tries to start an Android
Junit test that directly runs on the
emulator you can't use Junit4.
If you accidentally used the junit classes provided from the android jar they can't run on a normal jvm because there are only real compiled classes for the android dalvik vm.
This happened to me as well in Android Studio 1.1 - although it should support unit tests without a plugin.
On other machines (same project, same version of AS) I found that when running unit tests, the IDE does not add the android.jar file to the classpath, while in my machine it does.
My best guess was that due to the conversion we did from Maven to Gradle and moving from intellij to AS some cache of settings remained somewhere in my machine that caused android.jar to be added to the classpath.
What I did is to clear all android related caches from my machine (under the c:\users\USRE_NAME folder):
.android
.AndroidStudio
.gradle
.m2
After that I reopened the project and the tests worked.
Still trying to understand what went wrong, but this should do the trick for now.
I had this issue in Android Studio 1.5, because I did not know that I had to switch the "Test Artifact" setting in the "Build Variants" (lower left corner of the main window) from "Android Instrumentation Tests" to "Unit Tests". When you do, you can see an ExampleUnitTest.java file in the Project window.
I had the same problem but for another reason. I was on IntelliJ with a regular java gradle project (not android) but the JDK was set to the Android SDK in Project Structure (was the default JDK for some reasons). This is really dumb but IntelliJ wasn't nice enough to indicate me what's wrong, so I got stuck on that.
This is how I solved it:
Edit Configurations -> Defaults -> Android JUnit -> Add the following to Working Directory:
$MODULE_DIR$
Worked when I update IDEA version to 2021.2.1.
In Android project I had minifyEnabled = true, after I changed it to false everything worked.
If you remove
testOptions {
unitTests.returnDefaultValues = true
}
from your build.gradle it will work
Go to Project Structure -> Platform Setting, change SDKs to 1.8
solved my problem.
I followed CrazyCoder's answer but there was no junit file shown in dependencies. so i downloaded one from http://www.java2s.com/Code/Jar/j/Downloadjunitjar.htm, then added it by pressing the plus button on the right. And it worked
Turning off "Use embedded JDK" in Project Structure/SDK Location is what helped in my case but I don't know exactly what was the reason it was failing in the first place.
Replace your android.jar in libs folder with the latest one.
You can download it from here
In AndroidStudio, Open Project Structure -> SDK Location, you can see JDK location, change use "Use embedded JDK" to you own JDK to apply, then change back to "Use embedded JDK", it's maybe work
In my case, change JRE in Run Configurations dose solve the problem, but when I click the run button next to the test function, the JRE options will reset to default.
Finally, similar to #CrazyLiu 's answer, in Project Structure - SDK Location - JDK, select Embedded JDK. Because there is no checkbox in Android Studio 3.6.
None of the above worked for me (Intellij 2019.3.5
Build #IU-193.7288.26), finally using 're-import all projects' button on the maven pane worked.
For me, i did delete useLibrary 'android.test.runner' line in android {} block at bulid.gradle module file and everything worked fine.
I had the same problem in a Java 11 with Spring project, turns out when I tried to run the test, I put the wrong "shorten command" option.
Using the "JAR Manifest" option fixed the issue.
IntelliJ shorten command options
I was also facing the same issue, after changing into build.gradle it's working fine for me.
change your junit version inside build.gradle to:
testImplementation 'junit:junit:3.8'

Categories

Resources