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.59k stars 1.07k forks source link

Injected instance is null (Constructor Injection) #1288

Closed frudolph77 closed 2 years ago

frudolph77 commented 2 years ago

Describe the issue It seems that injecting MockServerClient into constructor is not working.

What you are trying to do I'm trying to use MockServer as a 'standalone' MockServer to run IntegrationTests.

I have added the following to the pom:**

<dependency>
    <groupId>org.mock-server</groupId>
    <artifactId>mockserver-junit-jupiter</artifactId>
    <version>5.13.2</version>
</dependency>

I'm not using ...-no-dependencies because it breaks the logging in the project, due to multiple SLJ4J bindings.

In my TestCase im using

@ExtendWith(MockServerExtension.class)
@MockServerSettings(ports = {3000})
public class OperatorTest extends AbstractOperatorTest {

    private final ClientAndServer client;

    public OperatorTest(ClientAndServer client) {
        this.client = client;
        client.when(
              request()
                  .withMethod("POST")
                  .withPath("/api/token")
                  .withBody("...")
          )
          .respond(
              response()
                  .withStatusCode(200)
                  .body(
                      // ...
                  )
          );
    }

    @Test
    void testSomething() {
        // ...
    }
}

MockServer version 5.13.2

To Reproduce Steps to reproduce the issue:

  1. How you are running MockServer as JUnit 5 Test Extension
  2. Code you used to create expectations Not relevant because of NullPointerException
  3. What error you saw NullPointerExecption at client.when()

Expected behaviour ClientAndServer or MockServerClient Instance in constructor not to be null

MockServer Log But in the LOG I see this

13:32:09.991 [MockServer-EventLog0] INFO  3000 started on port: 3000

So the MockServer is started, and I also see the logs of the incoming request when I disable the configuring of expectations

When using injection to @BeforeAll or test case the injected client is not null, but in the specific case I can't use these approaches. It's mandatory to init the MockServer in the Constructor of the TestClass.

jamesdbloom commented 2 years ago

I can see the issue you mentioned with multiple SLF4J bindings, it seems that the shaded plugins includes optional dependencies from the pom which seems like a bug in the shaded plugin to me.

I'm just testing a fix for that first before looking at the issue you mentioned above.

jamesdbloom commented 2 years ago

The SNAPSHOT version now no longer had the jdk14 SLF4J bindings in the shaded or no-dependencies jars.

jamesdbloom commented 2 years ago

I can't reproduce this error but you haven't provided the code for AbstractOperatorTest which I imagine is what may be causing the problem in you case.

If you provide the code for AbstractOperatorTest I may be able to understand why this is not working in your case.