Class RestStubsHook
- All Implemented Interfaces:
GuiceyConfigurationHook
ResourceExtension, but with full guice support. Rest extensions like exception
mappers, filters, etc. could also be disabled (including dropwizard default extensions). As guicey knows all
registered extensions, it provides them automatically (so, by default, no configuration is required - all jersey
resources and extensions are available).
It is not quite correct to call it stubs - because this is a fully functional rest (same as in normal application). It is called stub just to highlight customization ability (for example, we can start only resource with just a bunch of enabled extensions).
Could be used ONLY with lightweight guicey test
(TestSupport.runCoreApp(Class, String, String...).
Activates custom rest container, started on random port and with all rest resources and extensions. As test
container does not support web resources (will simply not work), all registered web extensions are disabled
(to avoid confusion by console output), together with GuiceFilter.
Rest client should be used to call rest: getRestClient(). Use it to call rest methods:
Something result = rest.get("/relative/rest/path", Something.class) (see
RestClient class for usage info).
To limit started rest resources, simply specify what resources to start (test could start only one resource to
test it): RestStubsRunner.builder().resources(Resources1.class, Resource2.class). Alternatively,
if many resources required, you can disable some resources:
RestStubsRunner.builder().disableResources(Resources1.class, Resource2.class).
By default, all jersey extensions, declared in application are applied. You can disable all of them:
@RestStubsRunner.builder().disableAllJerseyExtensions(true) (note that dropwizard extensions remain!).
Or you can specify just required extensions:
RestStubsRunner.builder().jerseyExtensions(Ext1.class, Ext2.class).
Also, only some extensions could be disabled:
RestStubsRunner.builder().disableJerseyExtensions(Ext1.class, Ext2.class).
Default dropwizard's exception mappers could be disabled with:
RestStubsRunner.builder().disableDropwizardExceptionMappers(true). This is very useful for testing rest
errors (to receive exception instead of generic 500 response).
By default, in-memory container (lightweight, but not all features supported) would be used and grizzly container,
if available in classpath. Use RestStubsRunner.builder().container(..) option to force the exact container
type (prevent incorrect usage).
The full list of enabled jersey extensions (including dropwizard and jersey core) could be seen with
.printJerseyConfig() option, activated in application (guice builder) or using a hook.
Log requests option (RestStubsRunner.builder().logRequests(true) activates complete requests and responses
logging.
Warn: the default guicey client (ClientSupport) would not work - but you
don't need it as a complete rest client provided.
- Since:
- 20.04.2025
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRest stubs configuration builder. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic RestStubsHook.Builderbuilder()voidconfigure(GuiceBundle.Builder builder) Configuration is applied just after manual configuration (through bundle's builder in application class).Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ru.vyarus.dropwizard.guice.hook.GuiceyConfigurationHook
register
-
Constructor Details
-
RestStubsHook
Create hook.- Parameters:
config- configuration
-
-
Method Details
-
builder
- Returns:
- builder to configure rest
-
configure
Description copied from interface:GuiceyConfigurationHookConfiguration is applied just after manual configuration (through bundle's builder in application class).GuiceBundle.Buildercontains special methods for test support:- Generic disable:
GuiceBundle.Builder.disable(java.util.function.Predicate[]) - Direct disable* method, for example
GuiceBundle.Builder.disableExtensions(Class[]) - Guice bindings override:
GuiceBundle.Builder.modulesOverride(com.google.inject.Module...)
GuiceBundle.Builder.option(Enum, Object)).All configuration items, registered with hook will be scoped as
GuiceyConfigurationHookinstead ofApplicationand so will be clearly distinguishable in configuration logs (GuiceBundle.Builder.printDiagnosticInfo()).- Specified by:
configurein interfaceGuiceyConfigurationHook- Parameters:
builder- just created bundle's builder- Throws:
Exception- on error (simplify usage)
- Generic disable:
-
getJerseyStub
- Returns:
- jersey test instance
-
getRestClient
- Returns:
- rest client, configured to call stubbed rest
-