public interface EmbeddedApp extends ApplicationUnderTest, java.lang.AutoCloseable
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.
LaunchConfigEmbeddedApp| Modifier and Type | Method and Description |
|---|---|
default void |
close()
Stops the server returned by
getServer(). |
static EmbeddedApp |
fromChain(ratpack.func.Action<? super ratpack.handling.Chain> action)
Creates an embedded application with a default launch config (no base dir, ephemeral port) and the given handler chain.
|
static EmbeddedApp |
fromHandler(ratpack.handling.Handler handler)
Creates an embedded application with a default launch config (no base dir, ephemeral port) and the given handler.
|
static EmbeddedApp |
fromHandler(java.nio.file.Path baseDir,
ratpack.handling.Handler handler)
Creates an embedded application with a default launch config (ephemeral port) and the given handler.
|
static EmbeddedApp |
fromHandlerFactory(ratpack.launch.HandlerFactory handlerFactory)
Creates an embedded application with a default launch config (no base dir, ephemeral port) and the given handler.
|
static EmbeddedApp |
fromHandlerFactory(java.nio.file.Path baseDir,
ratpack.launch.HandlerFactory handlerFactory)
Creates an embedded application with a default launch config (ephemeral port) and the given handler.
|
static EmbeddedApp |
fromLaunchConfigBuilder(ratpack.func.Function<? super ratpack.launch.LaunchConfigBuilder,? extends ratpack.launch.LaunchConfig> function)
Creates an embedded application by building a
LaunchConfig. |
static EmbeddedApp |
fromLaunchConfigBuilder(java.nio.file.Path baseDir,
ratpack.func.Function<? super ratpack.launch.LaunchConfigBuilder,? extends ratpack.launch.LaunchConfig> function)
Creates an embedded application by building a
LaunchConfig with the given base dir. |
default java.net.URI |
getAddress()
The address of the application under test, which is guaranteed to be accepting requests.
|
default TestHttpClient |
getHttpClient()
Creates a new test HTTP client that tests this embedded application.
|
ratpack.server.RatpackServer |
getServer()
The server for the application.
|
default void |
test(java.util.function.Consumer<? super TestHttpClient> consumer)
Provides the given consumer with a
test http client for this application, then closes this application. |
static EmbeddedApp fromLaunchConfigBuilder(ratpack.func.Function<? super ratpack.launch.LaunchConfigBuilder,? extends ratpack.launch.LaunchConfig> function)
LaunchConfig.
The given LaunchConfigBuilder will be configured to not have base dir, and to use an ephemeral port.
function - a function that builds a launch config from a launch config builderstatic EmbeddedApp fromLaunchConfigBuilder(java.nio.file.Path baseDir, ratpack.func.Function<? super ratpack.launch.LaunchConfigBuilder,? extends ratpack.launch.LaunchConfig> function)
LaunchConfig with the given base dir.
The given LaunchConfigBuilder will be configured to use an ephemeral port.
function - a function that builds a launch config from a launch config builderstatic EmbeddedApp fromHandlerFactory(ratpack.launch.HandlerFactory handlerFactory)
If you need to tweak the launch config, use fromLaunchConfigBuilder(Path, Function).
handlerFactory - a handler factorystatic EmbeddedApp fromHandlerFactory(java.nio.file.Path baseDir, ratpack.launch.HandlerFactory handlerFactory)
If you need to tweak the launch config, use fromLaunchConfigBuilder(Path, Function).
handlerFactory - a handler factorystatic EmbeddedApp fromHandler(ratpack.handling.Handler handler)
If you need to tweak the launch config, use fromLaunchConfigBuilder(Function).
handler - the application handlerstatic EmbeddedApp fromHandler(java.nio.file.Path baseDir, ratpack.handling.Handler handler)
If you need to tweak the launch config, use fromLaunchConfigBuilder(Path, Function).
handler - the application handlerstatic EmbeddedApp fromChain(ratpack.func.Action<? super ratpack.handling.Chain> action)
If you need to tweak the launch config, use fromLaunchConfigBuilder(Function).
action - the handler chain definitiondefault void test(java.util.function.Consumer<? super TestHttpClient> consumer)
test http client for this application, then closes this application.
The application will be closed regardless of whether the given consumer throws an exception.
import ratpack.test.embed.EmbeddedApp;
public class Example {
public static void main(String... args) {
EmbeddedApp.fromHandler(ctx -> ctx.render("ok"))
.test(httpClient -> {
assert httpClient.get().getBody().getText().equals("ok");
});
}
}
consumer - a consumer that tests this embedded applicationratpack.server.RatpackServer getServer()
Calling this method does not implicitly start the server.
default TestHttpClient getHttpClient()
default java.net.URI getAddress()
getAddress in interface ApplicationUnderTestdefault void close()
getServer().
Exceptions thrown by calling RatpackServer.stop() are suppressed and written to System.err.
close in interface java.lang.AutoCloseable