Class 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's 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