Skip to content

MockServer starting twice due to MockServerSettings #1977

@endlessoblivion

Description

@endlessoblivion

Describe the issue
When the MockServerExtension is extended (as in ExtendedMockServerExtension or ExtendedMockServerPerTestSuite) and used in a test in combination with MockServerSettings, then the MockServer starts twice in such a test, possibly competing for the same port (thus failing to bind).

Since the MockServer's JUnit 5 support doesn't provide port injection features as the Spring support does --- @MockServerPort and ${mockServerPort} --- the use of MockServerSettings with an explicit port is "mandatory" in auto-configured Spring Boot test.

IMHO the issue is due to MockServerSettings itself being annotated with @ExtendWith(MockServerExtension.class), which in itself seems strange, even if possibly convenient.

What you are trying to do
I extended the MockServerExtension to initialize expectations as early as possible --- earlier than Spring's OAuth2 initialization, which attempts to load some required OAuth2 metadata from a URL to be served by the MockServer.

The expectations are in a JSON file (and an OpenAPI YAML referenced from it), but I didn't find any other way how to make MockServer aware of it in a JUnit 5 test (expectations / files differ from one test class to another), because it doesn't look at the test properties such as mockserver.initializationJsonPath, when provied via @SpringBootTest(properties = "...").

A side note: The OAuth2 initialization happens too early, rendering the MockServerTestExecutionListener unusable (via @MockServerTest), because it starts the MockServer too late in a test's life-cycle. The MockServerExtension starts the MockServer early enough.

MockServer version
The current latest, e.g. org.mock-server:mockserver-junit-jupiter:5.15.0.

To Reproduce

  • with a random port --- started twice on different ports:
class ExtendedMockServerExtension : MockServerExtension()

@ExtendWith(ExtendedMockServerExtension::class)
@MockServerSettings
class Test {
    @Test
    fun test() {}
}
  • with an explicit port --- started twice on the same port:
class ExtendedMockServerExtension : MockServerExtension()

@ExtendWith(ExtendedMockServerExtension::class)
@MockServerSettings(ports = [1080])
class Test {
    @Test
    fun test() {}
}

Expected behaviour

  • with a random port:
[MockServer-EventLog0] INFO org.mockserver.log.MockServerEventLog -- 44663 started on port: 44663
  • with an explicit port
[MockServer-EventLog0] INFO org.mockserver.log.MockServerEventLog -- 1080 started on port: 1080

MockServer Log

  • with a random port --- started twice on different ports:
[MockServer-EventLog0] INFO org.mockserver.log.MockServerEventLog -- 44663 started on port: 44663
[MockServer-EventLog3] INFO org.mockserver.log.MockServerEventLog -- 45471 started on port: 45471
  • with an explicit port --- started twice on the same port:
[MockServer-EventLog0] INFO org.mockserver.log.MockServerEventLog -- 1080 started on port: 1080
[MockServer-EventLog3] ERROR org.mockserver.log.MockServerEventLog -- 1080 exception binding to port(s) [1080]
java.lang.RuntimeException: Exception while binding MockServer to port 1080
Caused by: java.net.BindException: Address already in use

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions