question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing" when importing wiremock 2.7.1 maven dependency

See original GitHub issue

Greetings,

After some research I decided to use wiremock in our project unit tests, which were working and running as expected before including wiremock 2.7.1 in the project’s pom.xml

my current test scope dependencies until now were the following (taken from the project’s BOM file)

    <!-- testing dependency versions -->
    <junit.version>4.12</junit.version>
    <assertj.version>3.4.0</assertj.version>
    <equalsverifier.version>2.1.7</equalsverifier.version>
    <openpojo.version>0.8.4</openpojo.version>

after including wiremock in the pom.xml file:

    <dependency>
      <groupId>com.github.tomakehurst</groupId>
      <artifactId>wiremock</artifactId>
      <version>2.7.1</version>
    </dependency>

all tests ceased to work, no matter which tool I tried to use

  • Eclipse Oxygen Release (4.7.0)
  • terminal mvn clean test

the following exception is being thrown:

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:10)
	at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
	at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
	at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
	at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 24 more

After inspecting the pom.xml file in wiremock 2.7.1 I think to have found the possible cause of this:

  • Wiremock is including it’s own JUnit library, which luckily matches the version I’m using but with the subtle difference that it is excluding hamcrest-core:
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>compile</scope>
      <exclusions>
        <exclusion>
          <artifactId>hamcrest-core</artifactId>
          <groupId>org.hamcrest</groupId>
        </exclusion>
      </exclusions>
    </dependency>

Questions

  • Why is wiremock delivering its own JUnit dependency in a <scope>compile</scope> scope?
  • And why is hamcrest-core being implicitly excluded from it?

The real question

  • How to workaround this?

I’m considering to exclude the junit dependency from my wiremock dependency, but not yet sure what side effects may arise from this.

Any pointers in the right direction are greatly welcome!

Thank you very much in advance.

PS: Removing the wiremock dependency fixes the problem for me. With the obvious downside that then I cannot use it 😃

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:9

github_iconTop GitHub Comments

2reactions
tomakehurstcommented, Sep 30, 2019

JUnit is now an compileOnly/provided dependency and excluded from the standalone JAR in 2.25.0. Hopefully that should resolve this issue.

2reactions
tomakehurstcommented, Sep 3, 2019

Thanks, I’ll take a look.

Read more comments on GitHub >

github_iconTop Results From Across the Web

org/hamcrest/SelfDescribing" when importing wiremock 2.7.1 ...
"java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing" when importing wiremock 2.7.1 maven dependency #743.
Read more >
Developers - "java.lang.NoClassDefFoundError: org/hamcrest ...
"java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing" when importing wiremock 2.7.1 maven dependency.
Read more >
NoClassDefFoundError on Maven dependency - Stack Overflow
I can run it on eclipse with m2eclipse plugin, I developed the application on it. I can't just figure out how to run...
Read more >
How to Download and Install WireMock
WireMock is distributed in two flavours - a standard JAR containing just WireMock, and a standalone fat JAR containing WireMock plus all its...
Read more >
Trouble Getting Started - Google Groups
It could very well be that I am new to Java as well. ... WireMockServer; import static com.github.tomakehurst.wiremock.core. ... List; import java.io.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found