mock-server / mockserver

MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Ruby. MockServer also includes a proxy that introspects all proxied traffic including encrypted SSL traffic and supports Port Forwarding, Web Proxying (i.e. HTTP proxy), HTTPS Tunneling Proxying (using HTTP CONNECT) and SOCKS Proxying (i.e. dynamic port forwarding).
http://mock-server.com
Apache License 2.0
4.57k stars 1.07k forks source link

`NoSuchMethodError` when starting mockserver with version >=5.13.1 #1273

Open martinfrancois opened 2 years ago

martinfrancois commented 2 years ago

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:

  1. Clone the minimal reproducible example here: https://github.com/martinfrancois/mockserver-5_13_1-repro
  2. Run the main method in src/main/java/MockServer.java
  3. Aforementioned error appears
  4. Run the unit tests in src/test/java/MockServerTest.java
  5. 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.

jamesdbloom commented 2 years ago

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.

jamesdbloom commented 2 years ago

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.

martinfrancois commented 2 years ago

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: