Class ContextMock

  • All Implemented Interfaces:
    io.javalin.router.EndpointExecutor

    @ApiStatus.Experimental() 
    public final class ContextMock
     implements EndpointExecutor
                        

    A ContextMock is an in-memory Context instance builder.

    Although this implementation can be used in different ways, the most common use case is to build a Context instance within the test scope. We strongly recommend using ContextMock over any reflection based mocking library, as it's way closer to the real implementation.

    By default, the request state represents an incoming connection from localhost to the root path. Javalin configuration, request and response states can be modified by using the ContextMockConfigurer interface. Once the state is prepared, you can build a Context instance in two ways:

    • ContextMock.build with Endpoint instance: to simulate a real request/response cycle (recommended)

    • ContextMock.execute: to execute non-endpoint related code that requires a Context instance

    See docs for more information: https://javalin.io/documentation#context-mock