Annotation Interface TestGuiceyApp
Note: in spite of the fact that jersey not started, Managed objects would
be started and stopped normally.
Extension may be declared on test class or on any nested class. When declared on nested class, applies to all lower nested classes (default junit 5 extension appliance behaviour). Multiple extension declarations (on multiple nested levels) is useless as junit will use only the top declared extension instance (and other will be ignored).
Guice context started before all tests (before BeforeAll) and shut down
after all tests (after AfterAll). If you need to restart application between tests
then declare extension in RegisterExtension non-static field instead of
annotation.
Guice injections will work on test fields annotated with Inject or
Inject (Injector.injectMembers(Object) applied on test instance).
Guice AOP will not work on test methods (because test itself is not created by guice).
Test constructor, lifecycle and test methods may use additional parameters:
- Any declared (possibly with qualifier annotation or generified) guice bean
- For not declared beans use
Jitto force JIT binding (create declared bean with guice, even if it wasn't registered) - Specially supported objects:
Applicationor exact application classObjectMapperClientSupportfor calling external urls
Internally use DropwizardTestSupport with custom command
(TestCommand).
It is possible to apply extension manually using RegisterExtension
and TestGuiceyAppExtension.forApp(Class) builder. For static field the behaviour would be the same as with
annotation, for non-static field application will restart before each test method.
- Since:
- 29.04.2020
-
Required Element Summary
Required Elements -
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionbooleanShortcut forclientFactory()to configureApacheTestClientFactory.Class<? extends TestClientFactory>Custom client factory forClientSupportobject.Class<? extends ConfigModifier<?>>[]Configuration modifier is an alternative for configuration override, which is limited for simple property types (for example, a collection could not be overridden).String[]Each value must be written askey: value.booleanEnables debug output for extension: used setup objects, hooks and applied config overrides.Class<? extends GuiceyConfigurationHook>[]Hooks provide access to guice builder allowing complete customization of application context in tests.booleanWhen test lifecycle isTestInstance.Lifecycle.PER_CLASSsame test instance used for all test methods.booleanBy default, guicey simulatesManagedobjects lifecycle.booleanBy default, a new application instance is started for each test.Class<? extends TestEnvironmentSetup>[]Environment support object is the simplest way to prepare additional objects for test (like database) and apply configuration overrides.boolean
-
Element Details
-
value
Class<? extends io.dropwizard.core.Application> value- Returns:
- application class
-
-
-
config
String config- Returns:
- path to configuration file (optional)
- Default:
- ""
-
configOverride
String[] configOverrideEach value must be written askey: value.In order to specify raw
ConfigOverridevalues (for delayed evaluation) use direct extension registration withRegisterExtensioninstead of annotation.- Returns:
- list of overridden configuration values (may be used even without real configuration)
- See Also:
- Default:
- {}
-
configModifiers
Class<? extends ConfigModifier<?>>[] configModifiersConfiguration modifier is an alternative for configuration override, which is limited for simple property types (for example, a collection could not be overridden).- Returns:
- configuration modifiers
- Default:
- {}
-
hooks
Class<? extends GuiceyConfigurationHook>[] hooksHooks provide access to guice builder allowing complete customization of application context in tests.For anonymous hooks you can simply declare hook as field:
@EnableHook static GuiceyConfigurationHook hook = builder -> builder.disableExtension(Something.class). Non-static fields may be used only when extension is registered with non-static field (static fields would be also counted in this case). All annotated fields will be detected automatically and objects registered. Fields declared in base test classes are also counted.- Returns:
- list of hooks to use
- See Also:
- Default:
- {}
-
setup
Class<? extends TestEnvironmentSetup>[] setupEnvironment support object is the simplest way to prepare additional objects for test (like database) and apply configuration overrides. Provided classes would be instantiated with the default constructor.To avoid confusion with guicey hooks: setup object required to prepare test environment before test (and apply required configurations) whereas hooks is a general mechanism for application customization (not only in tests).
Anonymous implementation could be simply declared as field:
@EnableSetup static TestEnvironmentSetup ext = ext -> ext.configOverrides("foo:1"). Non-static fields may be used only when extension is registered with non-static field (static fields would be also counted in this case). All annotated fields will be detected automatically and objects registered. Fields declared in base test classes are also counted.- Returns:
- setup objects to use
- See Also:
- Default:
- {}
-
injectOnce
boolean injectOnceWhen test lifecycle isTestInstance.Lifecycle.PER_CLASSsame test instance used for all test methods. By default, guicey would perform fields injection before each method because there might be prototype beans that must be refreshed for each test method. If you don't rely on prototypes, injections could be performed just once (for the first test method).- Returns:
- true to inject guice beans once per test instance, false otherwise
- Default:
- false
-
debug
boolean debugEnables debug output for extension: used setup objects, hooks and applied config overrides. Might be useful for concurrent tests too because each message includes configuration prefix (exactly pointing to context test or method).Also, shows guicey extension time, so if you suspect that guicey spent too much time, use the debug option to be sure. Performance report is published after each "before each" phase and after "after all" to let you see how extension time increased with each test method (for non-static guicey extension (executed per method), performance printed after "before each" and "after each" because before/after all not available)
Configuration overrides are printed after application startup (but before the test) because overridden values are resolved from system properties (applied by
DropwizardTestSupport.before()). If application startup failed, no configuration overrides would be printed (because dropwizard would immediately cleanup system properties). Using system properties is the only way to receive actually applied configuration value because property overrides might be implemented as value providers and potentially return different values.System property might be used to enable debug mode:
-Dguicey.extensions.debug=true. Or alias in code:TestSupport.debugExtensions().- Returns:
- true to enable debug output, false otherwise
- Default:
- false
-
reuseApplication
boolean reuseApplicationBy default, a new application instance is started for each test. If you want to re-use the same application instance between several tests, then put extension declaration in BASE test class and enable the reuse option: all tests derived from this base class would use the same application instance.You may have multiple base classes with reusable application declaration (different test hierarchies) - in this case, multiple applications would be kept running during tests execution.
All other extensions (without enabled re-use) will start new applications: take this into account to prevent port clashes with already started reusable apps.
Reused application instance would be stopped after all tests execution.
- Returns:
- true to reuse application, false to start application for each test
- Default:
- false
-
useDefaultExtensions
boolean useDefaultExtensionsDefault extensions:MockBean,StubBean,SpyBean,TrackBean,RecordLogs,StubRest.Disables service lookup for
TestEnvironmentSetup.By default, these extensions enabled and this option could disable them (if there are problems with them or fields analysis took too much time).
- Returns:
- true to use default extensions
- Default:
- true
-
clientFactory
Class<? extends TestClientFactory> clientFactoryCustom client factory forClientSupportobject. Custom factory may be required in case when custom client configuration is required for test.Core test does not start dropwizard web services, but client still could be used to call external services.
Note: value is ignored when
apacheClient()set to true- Returns:
- client factory class
- Default:
- ru.vyarus.dropwizard.guice.test.client.DefaultTestClientFactory.class
-
apacheClient
boolean apacheClientShortcut forclientFactory()to configureApacheTestClientFactory. The defaultHttpUrlConnectorProvidersupports only HTTP 1.1 methods and have problem with PATCH method usage on jdk > 16.Note:
clientFactory()value is ignored when set to true,Apache client is not set by default because of its problems with multipart: see jersey issue.
- Returns:
- true to use apache connection provider in jersey client
- Default:
- false
-
managedLifecycle
boolean managedLifecycleBy default, guicey simulatesManagedobjects lifecycle.It might be required in test to avoid starting managed objects (especially all managed in application) because important (for test) services replaced with mocks (and no need to wait for the rest of the application).
Note that
LifeCyclewould still be supported as internal events rely on it (it is assumed that the application use only managed objects to initialize logic).- Returns:
- true to simulate managed objects lifecycle, false to disable simulation
- Default:
- true
-