MokksyServerJava

Java-friendly wrapper around MokksyServer that provides a fully instance-method-based API.

Eliminates Kotlin-specific boilerplate from Java test code:

  • start() and shutdown() are instance methods

  • HTTP stub methods (get, post, etc.) accept Consumer<RequestSpecificationBuilder<P>> instead of Kotlin lambdas with receivers (no return Unit.INSTANCE)

  • Returns JavaBuildingStep, which exposes respondsWith and respondsWithStream as chainable instance methods (no separate MokksyJava.respondsWith(step, ...) call)

Example:

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class MyTest {
private final MokksyServerJava mokksy = new MokksyServerJava();

@BeforeAll void setUp() { mokksy.start(); }
@AfterAll void tearDown() { mokksy.shutdown(); }

@Test
void myTest() throws Exception {
mokksy.get(spec -> spec.path("/ping"))
.respondsWith(builder -> builder.setBody("Pong"));
// ... make HTTP call and assert
}
}

Constructors

Link copied to clipboard
constructor(delegate: MokksyServer)

Wraps an existing MokksyServer instance.

constructor(port: Int = 0, host: String = DEFAULT_HOST, verbose: Boolean = false)

Creates a MokksyServerJava backed by a new MokksyServer.

Properties

Link copied to clipboard

The underlying MokksyServer. Accessible for advanced configuration and for Kotlin callers that mix the two APIs.

Functions

Link copied to clipboard

Returns the base URL of the server in the form http://<host>:<port>.

Link copied to clipboard
open override fun close()

Closes the resource, releasing any underlying resources.

Link copied to clipboard

Registers a DELETE stub with a String request body.

Registers a DELETE stub with a String request body and StubConfiguration.

Registers a DELETE stub with a typed request body.

fun <P : Any> delete(configuration: StubConfiguration, requestType: Class<P>, spec: Consumer<RequestSpecificationBuilder<P>>): JavaBuildingStep<P>

Registers a DELETE stub with a typed request body and StubConfiguration.

Link copied to clipboard

Returns all requests that were not matched by any stub.

Link copied to clipboard

Returns all stubs that were never matched.

Link copied to clipboard

Registers a GET stub with a String request body.

Registers a GET stub with a String request body and StubConfiguration.

Registers a GET stub with a typed request body.

fun <P : Any> get(configuration: StubConfiguration, requestType: Class<P>, spec: Consumer<RequestSpecificationBuilder<P>>): JavaBuildingStep<P>

Registers a GET stub with a typed request body and StubConfiguration.

Link copied to clipboard

Registers a HEAD stub.

Registers a HEAD stub with StubConfiguration.

Registers a HEAD stub with a typed request body.

fun <P : Any> head(configuration: StubConfiguration, requestType: Class<P>, spec: Consumer<RequestSpecificationBuilder<P>>): JavaBuildingStep<P>

Registers a HEAD stub with a typed request body and StubConfiguration.

Link copied to clipboard

Registers an OPTIONS stub.

Registers an OPTIONS stub with StubConfiguration.

Registers an OPTIONS stub with a typed request body.

fun <P : Any> options(configuration: StubConfiguration, requestType: Class<P>, spec: Consumer<RequestSpecificationBuilder<P>>): JavaBuildingStep<P>

Registers an OPTIONS stub with a typed request body and StubConfiguration.

Link copied to clipboard

Registers a PATCH stub with a String request body.

Registers a PATCH stub with a String request body and StubConfiguration.

Registers a PATCH stub with a typed request body.

fun <P : Any> patch(configuration: StubConfiguration, requestType: Class<P>, spec: Consumer<RequestSpecificationBuilder<P>>): JavaBuildingStep<P>

Registers a PATCH stub with a typed request body and StubConfiguration.

Link copied to clipboard
fun port(): Int

Returns the port the server is bound to.

Link copied to clipboard

Registers a POST stub with a String request body.

Registers a POST stub with a String request body and StubConfiguration.

Registers a POST stub with a typed request body.

fun <P : Any> post(configuration: StubConfiguration, requestType: Class<P>, spec: Consumer<RequestSpecificationBuilder<P>>): JavaBuildingStep<P>

Registers a POST stub with a typed request body and StubConfiguration.

Link copied to clipboard

Registers a PUT stub with a String request body.

Registers a PUT stub with a String request body and StubConfiguration.

Registers a PUT stub with a typed request body.

fun <P : Any> put(configuration: StubConfiguration, requestType: Class<P>, spec: Consumer<RequestSpecificationBuilder<P>>): JavaBuildingStep<P>

Registers a PUT stub with a typed request body and StubConfiguration.

Link copied to clipboard

Resets stub match counts and clears the request journal.

Link copied to clipboard
fun shutdown(gracePeriodMillis: Long = 500, timeoutMillis: Long = 1000)

Stops the server and blocks the calling thread until shutdown is complete.

Link copied to clipboard
fun start()

Starts the server and blocks the calling thread until the port is bound and the server is ready to accept requests.

Link copied to clipboard

Asserts that every request received by the server was matched by a stub.

Link copied to clipboard

Asserts that every registered stub was matched at least once.