com.asarkar.spring:mockserver-spring

Starts MockServer and makes the port available as Spring Boot environment property

License

License

GroupId

GroupId

com.asarkar.spring
ArtifactId

ArtifactId

mockserver-spring
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

pom.sha512
Description

Description

com.asarkar.spring:mockserver-spring
Starts MockServer and makes the port available as Spring Boot environment property
Project URL

Project URL

https://github.com/asarkar/mockserver-spring
Source Code Management

Source Code Management

https://github.com/asarkar/mockserver-spring

Download mockserver-spring

Dependencies

compile (1)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib-jdk8 jar 1.4.10

runtime (2)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-test jar
org.mock-server : mockserver-netty jar 5.11.2

Project Modules

There are no modules declared in this project.

mockserver-spring

Starts a MockServer and makes the port available as Spring Boot environment property. Stops the server when the Spring context is destroyed.

Requires Java 8 or later. Uses mockserver and Spring Boot.

Installation

You can find the latest version on Maven Central.

Usage

The only thing you need is the AutoConfigureMockServer annotation:

@SpringBootTest
@AutoConfigureMockServer
public class AutoConfigureWithKnownPortsTest {
    @BeforeEach
    // create expectation using port 1080

    @Test
    public void testConnection() {
        // GET "http://localhost:1080/"
    }
}

To use random port:

@SpringBootTest
@AutoConfigureMockServer(port = 0)
public class AutoConfigureWithRandomPortsTest {
    @Value("${mockserver.port:-1}")
    private int port;

    @BeforeEach
    // create expectation using port

    @Test
    public void testConnection() {
        // GET "http://localhost:port/"
    }
}

If you want to configure the MockServer before it's started, provide an implementation for MockServerConfigurationProvider , as a Spring bean.

See KDoc for more details.

Gotchas

  1. Do not create expectations in the static beforeAll() method; it runs before the MockServer starts. If you want to create commons expectations, either do it in the beforeEach() method, which runs before each test method, or do it in a non static beforeAll() method. In the second case, you'll have to also annotate your test class with @TestInstance(Lifecycle.PER_CLASS).

  2. In order for the server to be started, the class given to @SpringBootTest must be annotated with @SpringBootApplication. Such a class can be specified in the classes attribute of @SpringBootTest annotation, or found implicitly by component scan.

Contribute

This project is a volunteer effort. You are welcome to send pull requests, ask questions, or create issues. If you like it, you can help by spreading the word and "Starring" the GitHub repo!

License

Copyright 2021 Abhijit Sarkar - Released under Apache License v2.0.

Versions

Version
1.0.1
1.0.0