DropWizard - SoftwareSandbox/Fiazard GitHub Wiki

##IntegrationTesting whether all Exceptions are transformed into ErrorR's

Attempted writing an IntegrationTest using the DropwizardAppRule, and we'd need to somehow be able to register a Resource that would be "configurable" during test run. This won't work since it's an @ClassRule and the DropwizardApp is already up and running even when you enter your own @BeforeClass initialization block.

So, the only way we can integration test this is by checking all specific Exceptions that can get thrown on all specific resource methods. Or be content with just unit testing the ExceptionToJSONMapper which'll do the actual work anyway. We could add to that a unit test that checks that this mapper is registered as a provider on a running FiazardApp instance.

##Requiring Configuration during Guice Module initialisation The code you'll have in a class implementing com.google.inject.Module:

@Provides
@Singleton
public WhateverClass provideWhateverClass(FiazardConfig config) {
    config.get...;
}

The message you will get:

ERROR [2015-03-17 23:24:06,360] com.hubspot.dropwizard.guice.GuiceBundle: Exception occurred when creating Guice Injector - exiting
! com.google.inject.CreationException: Unable to create injector, see the following errors:
! 
! 1) The dropwizard environment has not yet been set. This is likely caused by trying to access the dropwizard environment during the bootstrap phase.
!   at com.hubspot.dropwizard.guice.DropwizardEnvironmentModule.configure(DropwizardEnvironmentModule.java:23)
!   while locating 

Is because the module is trying to get initialized in DropWizards bootstrap phase (App.init()), where any module doesn't have access to a Configuration yet. The fix is to remove the @Singleton and use old-skool lazy loading with a null check.

##The sadness that is adding HealthChecks Adding Resources works by passing YourResource.class as an argument to jersey().register();. This means that jersey figures out how to create (and inject necessary dependencies) on the resource to register. Too bad this doesn't work the same way for HealthChecks:

environment.healthChecks().register("MongoDBHealthCheck", guiceBundle.getInjector().getInstance(MongoDBHealthCheck.class));

Means you have to manually construct a new HealthCheck through guice (to have its dependencies injected) and get an instance that you can actually pass along to healthChecks().register(). Sad indeed.