public class ServiceTest extends Object
Dependencies to other services must be replaced by stub or mock implementations by
overriding the bindings of the GuiceApplicationBuilder
in the Setup
.
The server is ran standalone without persistence, pubsub or cluster features
enabled. Cassandra is also disabled by default. If your service require either of these features you
can enable them in the Setup
.
There are two different styles that can be used. It is most convenient to use withServer
,
since it automatically starts and stops the server before and after the given lambda.
When your test have several test methods, and especially when using persistence, it is
faster to only start
the server once in a static method annotated with @BeforeClass
and stop it in a method annotated with @AfterClass
.
Modifier and Type | Class and Description |
---|---|
static interface |
ServiceTest.Setup |
static class |
ServiceTest.TestServer
When the server is started you can get the service client and other
Guice bindings here.
|
Constructor and Description |
---|
ServiceTest() |
Modifier and Type | Method and Description |
---|---|
static <T> play.api.inject.BindingKey<T> |
bind(Class<T> clazz)
Create a binding that can be used with the
GuiceApplicationBuilder
in the Setup , e.g. |
static ServiceTest.Setup |
defaultSetup()
The default
Setup configuration, which has persistence enabled. |
static void |
eventually(scala.concurrent.duration.FiniteDuration max,
akka.japi.function.Effect block)
Retry the give
block (lambda) until it does not throw an exception or the timeout
expires, whichever comes first. |
static void |
eventually(scala.concurrent.duration.FiniteDuration max,
scala.concurrent.duration.FiniteDuration interval,
akka.japi.function.Effect block)
Retry the give
block (lambda) until it does not throw an exception or the timeout
expires, whichever comes first. |
static ServiceTest.TestServer |
startServer(ServiceTest.Setup setup)
Start the test server with the given
setup . |
static void |
withServer(ServiceTest.Setup setup,
akka.japi.function.Procedure<ServiceTest.TestServer> block)
Start the test server with the given
setup and run the block (lambda). |
public static ServiceTest.Setup defaultSetup()
Setup
configuration, which has persistence enabled.public static void withServer(ServiceTest.Setup setup, akka.japi.function.Procedure<ServiceTest.TestServer> block)
setup
and run the block
(lambda). When
the block returns or throws the test server will automatically be stopped.
This method should be used when the server can be started and stopped for each test
method. When your test have several test methods, and especially when using persistence, it is
faster to only start the server once with startServer(com.lightbend.lagom.javadsl.testkit.ServiceTest.Setup)
.
You can get the service client from the TestServer
that is passed as parameter
to the block
.
public static ServiceTest.TestServer startServer(ServiceTest.Setup setup)
setup
. You must stop the server with
the stop
method of the returned TestServer
when the test is finished.
When your test have several test methods, and especially when using persistence, it is
faster to only start the server once in a static method annotated with @BeforeClass
and stop it in a method annotated with @AfterClass
. Otherwise withServer
is
more convenient.
You can get the service client from the returned TestServer
.
public static void eventually(scala.concurrent.duration.FiniteDuration max, akka.japi.function.Effect block)
block
(lambda) until it does not throw an exception or the timeout
expires, whichever comes first. If the timeout expires the last exception
is thrown. The block
is retried with 100 milliseconds interval.public static void eventually(scala.concurrent.duration.FiniteDuration max, scala.concurrent.duration.FiniteDuration interval, akka.japi.function.Effect block)
block
(lambda) until it does not throw an exception or the timeout
expires, whichever comes first. If the timeout expires the last exception
is thrown. The block
is retried with the given interval
.public static <T> play.api.inject.BindingKey<T> bind(Class<T> clazz)
GuiceApplicationBuilder
in the Setup
, e.g. to override bindings to stub out dependencies to
other services.