mockserver
mockserver copied to clipboard
`NoSuchMethodError` when starting mockserver with version >=5.13.1
Describe the issue
When I'm trying to start the mockserver with version 5.13.1 or 5.13.2, it results in a NoSuchMethodError
, which does NOT appear with 5.13.0:
Exception in thread "main" java.lang.NoSuchMethodError: org.mockserver.mock.action.http.HttpActionHandler.<init>(Lorg/mockserver/configuration/Configuration;Lio/netty/channel/EventLoopGroup;Lorg/mockserver/mock/HttpState;Ljava/util/List;Lorg/mockserver/socket/tls/NettySslContextFactory;)V
at org.mockserver.netty.MockServer.createServerBootstrap(MockServer.java:189)
at org.mockserver.netty.MockServer.<init>(MockServer.java:78)
at org.mockserver.netty.MockServer.<init>(MockServer.java:57)
at org.mockserver.integration.ClientAndServer.<init>(ClientAndServer.java:58)
at org.mockserver.integration.ClientAndServer.<init>(ClientAndServer.java:52)
at MockServer.start(MockServer.java:10)
at MockServer.main(MockServer.java:6)
What you are trying to do
When running a main method that does ClientAndServer client = new ClientAndServer(1080);
, it results in the aforementioned error.
A similar error appears when trying to start junit tests when using the MockServerExtension
, I assume it's the same kind of issue, but wanted to mention it anyways in case it's a different issue.
MockServer version 5.13.1 and 5.13.2
To Reproduce Steps to reproduce the issue:
- Clone the minimal reproducible example here: https://github.com/martinfrancois/mockserver-5_13_1-repro
- Run the main method in
src/main/java/MockServer.java
- Aforementioned error appears
- Run the unit tests in
src/test/java/MockServerTest.java
- Similar
NoSuchMethodError
appears
Expected behaviour The mockserver to start normally, as when the mockserver version is 5.13.0, with an output like this:
2022-04-08 16:04:44 5.13.0 INFO 1080 started on port: 1080
MockServer Log No log output by MockServer, apart from the error.
It looks like your using a mix of shaded and not shaded dependencies which is likely the issue.
Can you only use shaded or non shaded dependencies.
Also you don't need to specify the client if you have pulled in mockserver-netty
as it includes the client.
Once you can confirm this is the issue I can make it clearer in the documentation to only use one or the other type of dependence. The shading process re-writes packages so if you include both there is a race condition in the class loader as to which class is loaded and you're have a mix of re-written classes and non-re-written classes.
Thanks a lot for getting back to me so quickly! Ah, I wasn't even aware that using the non-shaded ones was an option, in the documentation for 5.12.0 I remember it used to recommend somewhere to use the shaded ones for the client.
I can indeed confirm that switching to unshaded dependencies fixes this issue. In my case I was able to reduce it to the following (in case someone else runs into the same issue):
implementation("org.mock-server:mockserver-netty-no-dependencies:$mockserver_version")
testImplementation("org.mock-server:mockserver-junit-jupiter:$mockserver_version")
I have some suggestions for the docs that could make it a bit clearer, just to give you a perspective of an outsider:
- I didn't even look at the "Maven" page of the docs as we're using Gradle, so I thought I wouldn't find any information that would be relevant there, but just saw that there's even a Gradle section there. I think the title is quite misleading, I'd suggest changing it to something like "Java Dependencies" (or something like that)
- You mention in the docs in the sections where a reference is made on how to include the dependency using Maven the following sentence "For more details about the different dependency versions see the page on Maven Central". I actually didn't even consider clicking on the link as I understood "Maven Central" as a link to a page like this and understood "dependency versions" as in "5.13.0", "5.13.1" etc. (which I wasn't interested in), and not as you intended the your docs page where versions means something like "shaded","non-shaded", "no-dependencies" etc. Maybe you could rephrase it to be something like:
For more details on the different variants/variations that are available of the dependency, check the page on Java dependencies
. Maybe you have an even better idea, but I feel like especially the term "version" is already too reserved to be used in this context (even though of course, now that I understand what you mean I see why you went for it in the first place 😉 ) - I would mention somewhere that the
netty
dependency already includes theclient
, or maybe even more in general mention which dependencies include what already so people don't unnecessarily declare overlapping dependencies (pardon if it's mentioned somewhere, I couldn't find it at least) - I would also warn somewhere about using shaded and unshaded dependencies together, mentioning it could result in the issue you described in your comment, to make sure people (like me) don't do it by accident.
- On the getting started page, when clicking on "MockServer is available in the following formats: java dependency" this currently leads to the shaded netty version. I think this should instead lead to the "Maven" page of the docs