public interface EmbeddedApp extends ApplicationUnderTest, 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(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 |
fromHandler(Handler handler)
Creates an embedded application with a default launch config (no base dir, ephemeral port) and the given handler.
|
static EmbeddedApp |
fromHandler(Path baseDir,
Handler handler)
Creates an embedded application with a default launch config (ephemeral port) and the given handler.
|
static EmbeddedApp |
fromHandlerFactory(HandlerFactory handlerFactory)
Creates an embedded application with a default launch config (no base dir, ephemeral port) and the given handler.
|
static EmbeddedApp |
fromHandlerFactory(Path baseDir,
HandlerFactory handlerFactory)
Creates an embedded application with a default launch config (ephemeral port) and the given handler.
|
static EmbeddedApp |
fromLaunchConfigBuilder(Function<? super LaunchConfigBuilder,? extends LaunchConfig> function)
Creates an embedded application by building a
LaunchConfig . |
static EmbeddedApp |
fromLaunchConfigBuilder(Path baseDir,
Function<? super LaunchConfigBuilder,? extends LaunchConfig> function)
Creates an embedded application by building a
LaunchConfig with the given base dir. |
default 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.
|
RatpackServer |
getServer()
The server for the application.
|
default void |
test(Consumer<? super TestHttpClient> consumer)
Provides the given consumer with a
test http client for this application, then closes this application. |
static EmbeddedApp fromLaunchConfigBuilder(Function<? super LaunchConfigBuilder,? extends 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(Path baseDir, Function<? super LaunchConfigBuilder,? extends 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(HandlerFactory handlerFactory)
If you need to tweak the launch config, use fromLaunchConfigBuilder(Path, Function)
.
handlerFactory
- a handler factorystatic EmbeddedApp fromHandlerFactory(Path baseDir, HandlerFactory handlerFactory)
If you need to tweak the launch config, use fromLaunchConfigBuilder(Path, Function)
.
handlerFactory
- a handler factorystatic EmbeddedApp fromHandler(Handler handler)
If you need to tweak the launch config, use fromLaunchConfigBuilder(Function)
.
handler
- the application handlerstatic EmbeddedApp fromHandler(Path baseDir, Handler handler)
If you need to tweak the launch config, use fromLaunchConfigBuilder(Path, Function)
.
handler
- the application handlerstatic EmbeddedApp fromChain(Action<? super Chain> action)
If you need to tweak the launch config, use fromLaunchConfigBuilder(Function)
.
action
- the handler chain definitiondefault void test(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 applicationRatpackServer getServer()
Calling this method does not implicitly start the server.
default TestHttpClient getHttpClient()
default URI getAddress()
getAddress
in interface ApplicationUnderTest
default void close()
getServer()
.
Exceptions thrown by calling RatpackServer.stop()
are suppressed and written to System.err
.
close
in interface AutoCloseable