Class RestStubsHook
- java.lang.Object
-
- ru.vyarus.dropwizard.guice.test.rest.RestStubsHook
-
- All Implemented Interfaces:
GuiceyConfigurationHook
public class RestStubsHook extends java.lang.Object implements GuiceyConfigurationHook
Resources stubbing: start lightweight rest container (with, probably, only one or a couple of services to test) without web (no servlets, filters, etc. would work). This is the same as dropwizard'sResourceExtension, 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 withGuiceFilter.Rest client should be used to call rest:
getRestClient(). Use it to call rest methods:Something result = rest.get("/relative/rest/path", Something.class)(seeRestClientclass 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 Classes Modifier and Type Class Description static classRestStubsHook.BuilderRest stubs configuration builder.
-
Constructor Summary
Constructors Constructor Description RestStubsHook(StubRestConfig config)Create hook.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static RestStubsHook.Builderbuilder()voidconfigure(GuiceBundle.Builder builder)Configuration is applied just after manual configuration (through bundle's builder in application class).GuiceyJerseyTestgetJerseyStub()RestClientgetRestClient()-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface ru.vyarus.dropwizard.guice.hook.GuiceyConfigurationHook
register
-
-
-
-
Constructor Detail
-
RestStubsHook
public RestStubsHook(StubRestConfig config)
Create hook.- Parameters:
config- configuration
-
-
Method Detail
-
builder
public static RestStubsHook.Builder builder()
- Returns:
- builder to configure rest
-
configure
public void configure(GuiceBundle.Builder builder) throws java.lang.Exception
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:
java.lang.Exception- on error (simplify usage)
- Generic disable:
-
getJerseyStub
public GuiceyJerseyTest getJerseyStub()
- Returns:
- jersey test instance
-
getRestClient
public RestClient getRestClient()
- Returns:
- rest client, configured to call stubbed rest
-
-