public interface EmbeddedApp extends CloseableApplicationUnderTest
This mechanism can be used for functionally testing isolated sections of an application, or for testing general libraries that provide reusable functionality (e.g. Ratpack Guice modules).
Different implementations expose different API that can be used to define the actual application under test.
As embedded applications also implement ApplicationUnderTest
, they are suitable for use with clients accessing the app via HTTP.
Implementations must ensure that the application is up and receiving request when returning from getAddress()
.
Be sure to close()
the application after use to free resources.
Modifier and Type | Method and Description |
---|---|
default void |
close()
Stops the server returned by
getServer() . |
static EmbeddedApp |
fromHandler(Handler handler)
Creates an embedded application with a default launch config (no base dir, ephemeral port) and the given handler.
|
static EmbeddedApp |
fromHandlerFactory(Function<? super Registry,? extends Handler> handlerFactory)
Creates an embedded application with a default launch config (no base dir, ephemeral port) and the given handler.
|
static EmbeddedApp |
fromHandlers(Action<? super Chain> action)
Creates an embedded application with a default launch config (no base dir, ephemeral port) and the given handler chain.
|
static EmbeddedApp |
fromServer(Factory<? extends RatpackServer> server)
Creates an embedded application for the given server.
|
static EmbeddedApp |
fromServer(RatpackServer server)
Creates an embedded application for the given server.
|
static EmbeddedApp |
fromServer(ServerConfig serverConfig,
Action<? super RatpackServerSpec> definition)
Creates an embedded application using the given server config, and server creating function.
|
static EmbeddedApp |
fromServer(ServerConfigBuilder serverConfig,
Action<? super RatpackServerSpec> definition)
Creates an embedded application using the given server config, and server creating function.
|
default java.net.URI |
getAddress()
The address of the application under test, which is guaranteed to be listening for requests.
|
RatpackServer |
getServer()
The server for the application.
|
static EmbeddedApp |
of(Action<? super RatpackServerSpec> definition)
Creates an embedded application from the given function.
|
test
getHttpClient
static EmbeddedApp fromServer(RatpackServer server)
server
- the server to embedstatic EmbeddedApp of(Action<? super RatpackServerSpec> definition) throws java.lang.Exception
definition
- a function that defines the serverjava.lang.Exception
- if an error is encountered creating the applicationRatpackServer.of(Action)
static EmbeddedApp fromServer(Factory<? extends RatpackServer> server)
server
- a factory that creates the server to embedstatic EmbeddedApp fromServer(ServerConfigBuilder serverConfig, Action<? super RatpackServerSpec> definition)
serverConfig
- the server configurationdefinition
- a function to create the server to embedstatic EmbeddedApp fromServer(ServerConfig serverConfig, Action<? super RatpackServerSpec> definition)
serverConfig
- the server configurationdefinition
- a function to create the server to embedstatic EmbeddedApp fromHandlerFactory(Function<? super Registry,? extends Handler> handlerFactory)
If you need to tweak the server config, use fromServer(ServerConfig, Action)
.
handlerFactory
- a handler factorystatic EmbeddedApp fromHandler(Handler handler)
If you need to tweak the server config, use fromServer(ServerConfig, Action)
.
handler
- the application handlerstatic EmbeddedApp fromHandlers(Action<? super Chain> action)
If you need to tweak the server config, use fromServer(ServerConfig, Action)
.
action
- the handler chain definitionRatpackServer getServer()
Calling this method does not implicitly start the server.
default java.net.URI getAddress()
Implementations should start the application if it has not already been started before returning from this method.
getAddress
in interface ApplicationUnderTest
default void close()
getServer()
.
Exceptions thrown by calling RatpackServer.stop()
are suppressed and written to System.err
.
close
in interface java.lang.AutoCloseable
close
in interface CloseableApplicationUnderTest