Class TestSupportBuilder<C extends io.dropwizard.Configuration>
- java.lang.Object
-
- ru.vyarus.dropwizard.guice.test.builder.BaseBuilder<C,TestSupportBuilder<C>>
-
- ru.vyarus.dropwizard.guice.test.builder.TestSupportBuilder<C>
-
- Type Parameters:
C
- configuration type
public class TestSupportBuilder<C extends io.dropwizard.Configuration> extends BaseBuilder<C,TestSupportBuilder<C>>
Builder and runner forDropwizardTestSupport
andGuiceyTestSupport
objects. Allows using all available options. This builder should be suitable for cases when junit 5 extensions could not be used.Use
TestSupport.build(Class)
to build instance.Builder is not supposed to be used for multiple runs: registered hooks will be applied only once. This limitation is not possible to avoid because builder could be used for support objects creation, which are not aware of hooks. So hooks could be registered globally only in time of addition to the builder.
- Since:
- 14.11.2023
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
TestSupportBuilder.TestListener<C extends io.dropwizard.Configuration>
Listener forTestSupport.build(Class)
builder.
-
Field Summary
-
Fields inherited from class ru.vyarus.dropwizard.guice.test.builder.BaseBuilder
app, configObject, configOverrides, configPath, configSourceProvider, propertyPrefix
-
-
Constructor Summary
Constructors Constructor Description TestSupportBuilder(java.lang.Class<? extends io.dropwizard.Application<C>> app)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description GuiceyTestSupport<C>
buildCore()
Build a test support object with web servicesDropwizardTestSupport
.io.dropwizard.testing.DropwizardTestSupport<C>
buildWeb()
Build a test support object with web servicesDropwizardTestSupport
.TestSupportBuilder<C>
clientFactory(TestClientFactory factory)
Custom client factory implementation used forClientSupport
object creation (this special client class automatically constructs base urls for application under test, based on its configuration).TestSupportBuilder<C>
listen(TestSupportBuilder.TestListener<C> listener)
Listener used ONLY when builder run methods used!TestSupportBuilder<C>
randomPorts()
Shortcut to enable random web ports.TestSupportBuilder<C>
randomPorts(boolean randomPorts)
Use random http ports (applicable only for web).RunResult<C>
runCore()
Start and stop application without web services.<T> T
runCore(TestSupport.RunCallback<T> action)
Start and stop application without web services.RunResult<C>
runWeb()
Start and stop application with web services.<T> T
runWeb(TestSupport.RunCallback<T> action)
Start and stop application with web services.-
Methods inherited from class ru.vyarus.dropwizard.guice.test.builder.BaseBuilder
config, config, configOverride, configOverride, configOverride, configOverrides, configSourceProvider, hooks, hooks, prepareOverrides, propertyPrefix, restMapping
-
-
-
-
Constructor Detail
-
TestSupportBuilder
public TestSupportBuilder(java.lang.Class<? extends io.dropwizard.Application<C>> app)
-
-
Method Detail
-
randomPorts
public TestSupportBuilder<C> randomPorts()
Shortcut to enable random web ports.- Returns:
- builder instance for chained calls
-
randomPorts
public TestSupportBuilder<C> randomPorts(boolean randomPorts)
Use random http ports (applicable only for web). Useful to separate concurrent web instances runs.- Returns:
- builder instance for chained calls
-
clientFactory
public TestSupportBuilder<C> clientFactory(TestClientFactory factory)
Custom client factory implementation used forClientSupport
object creation (this special client class automatically constructs base urls for application under test, based on its configuration).Client instance could be accessed at any time (during test) with
TestSupportHolder.getClient()
- Parameters:
factory
- factory instance- Returns:
- builder instance for chained calls
-
listen
public TestSupportBuilder<C> listen(TestSupportBuilder.TestListener<C> listener)
Listener used ONLY when builder run methods used! Listener may be used to perform additional initialization or cleanup before/after application execution.- Parameters:
listener
- execution listener- Returns:
- builder instance for chained calls
-
buildCore
public GuiceyTestSupport<C> buildCore()
Build a test support object with web servicesDropwizardTestSupport
. Method supposed to be used only byTestSupport
for support objects creation. Prefer direct run (runCore()
) method usage (used support object could be easily obtained withTestSupport.getContext()
in any place).IMPORTANT: listeners could not be used (because they are implemented as a custom run callback)
- Returns:
- guicey test support implementation
-
buildWeb
public io.dropwizard.testing.DropwizardTestSupport<C> buildWeb()
Build a test support object with web servicesDropwizardTestSupport
. Method supposed to be used only byTestSupport
for support objects creation. Prefer direct run (runWeb()
) method usage (used support object could be easily obtained withTestSupport.getContext()
in any place).IMPORTANT: listeners could not be used (because they are implemented as a custom run callback)
- Returns:
- dropwizard test support implementation
-
runCore
public RunResult<C> runCore() throws java.lang.Exception
Start and stop application without web services. Mostly useful to test application startup errors (with proper application shutdown).NOTE: method not supposed to be used for multiple calls. For example, registered hooks would only work on first execution.
- Returns:
- run(webApp(appClass, configPath, overrides)
- Throws:
java.lang.Exception
- any appeared exception (throws may easily be added directly to test method and, without extra exception wrapper, we get exact exceptions as they would be thrown in real application)
-
runCore
public <T> T runCore(@Nullable TestSupport.RunCallback<T> action) throws java.lang.Exception
Start and stop application without web services. Provided action would be executed in time of application life.NOTE: method not supposed to be used for multiple calls. For example, registered hooks would only work on first execution.
- Type Parameters:
T
- result type- Parameters:
action
- action to execute while the application is running- Returns:
- action result
- Throws:
java.lang.Exception
- any appeared exception (throws may easily be added directly to test method and, without extra exception wrapper, we get exact exceptions as they would be thrown in real application)
-
runWeb
public RunResult<C> runWeb() throws java.lang.Exception
Start and stop application with web services. Mostly useful to test application startup errors (with proper application shutdown).NOTE: method not supposed to be used for multiple calls. For example, registered hooks would only work on first execution.
- Returns:
- test support object used for execution (for example, to examine configuration)
- Throws:
java.lang.Exception
- any appeared exception (throws may easily be added directly to test method and, without extra exception wrapper, we get exact exceptions as they would be thrown in real application)
-
runWeb
public <T> T runWeb(@Nullable TestSupport.RunCallback<T> action) throws java.lang.Exception
Start and stop application with web services. Provided action would be executed in time of application life.NOTE: method not supposed to be used for multiple calls. For example, registered hooks would only work on first execution.
- Type Parameters:
T
- result type- Parameters:
action
- action to execute while the application is running- Returns:
- action result
- Throws:
java.lang.Exception
- any appeared exception (throws may easily be added directly to test method and, without extra exception wrapper, we get exact exceptions as they would be thrown in real application)
-
-