DropWizardApplicationRunner { @SuppressWarnings("unchecked") public static <T extends Configuration> DropWizardServer<T> createServer(T config, Class<? extends Application<T>> applicationClass) throws Exception { final Application<T> application = applicationClass.getConstructor().newInstance(); final ServerCommand<T> serverCommand = new ServerCommand<T>(application); final Bootstrap<T> bootstrap = new Bootstrap<T>(application); bootstrap.addCommand(serverCommand); File tmpConfigFile = new File( System.getProperty("java.io.tmpdir"), config.getClass().getCanonicalName() + "_" + System.currentTimeMillis()); tmpConfigFile.deleteOnExit(); bootstrap.getObjectMapper().writeValue(tmpConfigFile, config); ConfigurationFactory<T> configurationFactory = bootstrap.getConfigurationFactoryFactory() .create((Class<T>) config.getClass(), bootstrap.getValidatorFactory().getValidator(), bootstrap.getObjectMapper(), "dw"); final T builtConfig = configurationFactory.build( bootstrap.getConfigurationSourceProvider(), tmpConfigFile.getAbsolutePath()); builtConfig.getLoggingFactory() .configure(bootstrap.getMetricRegistry(), bootstrap.getApplication().getName()); final Environment environment = new Environment(bootstrap.getApplication().getName(), bootstrap.getObjectMapper(), bootstrap.getValidatorFactory().getValidator(), bootstrap.getMetricRegistry(), bootstrap.getClassLoader()); builtConfig.getMetricsFactory().configure(environment.lifecycle(), bootstrap.getMetricRegistry()); final Server server = builtConfig.getServerFactory().build(environment); server.addLifeCycleListener(new AbstractLifeCycle.AbstractLifeCycleListener() { @Override public void lifeCycleStopped(LifeCycle event) { builtConfig.getLoggingFactory().stop(); } }); return new DropWizardServer(builtConfig, bootstrap, application, environment, server, environment.metrics()); }  @SuppressWarnings("unchecked") static DropWizardServer<T> createServer(T config, Class<? extends Application<T>> applicationClass);  }
@Test public void testCreateServer() throws Exception { ThirdEyeApplication.Config config = new ThirdEyeApplication.Config(); config.setRootDir(ROOT_DIR.getAbsolutePath()); DropWizardApplicationRunner.DropWizardServer<ThirdEyeApplication.Config> server = DropWizardApplicationRunner.createServer(config, ThirdEyeApplication.class); Assert.assertNotNull(server.getMetricRegistry()); server.start(); long startTime = System.currentTimeMillis(); boolean success = false; while (System.currentTimeMillis() - startTime < REQUEST_TIMEOUT_MILLIS) { try { HttpURLConnection conn = (HttpURLConnection) URI.create("http: byte[] res = IOUtils.toByteArray(conn.getInputStream()); if (Arrays.equals(res, "GOOD".getBytes())) { success = true; break; } } catch (Exception e) { } } Assert.assertTrue(success); server.stop(); }