Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DAO Testing with Dropwizard hibernate
package com.rtr.infra.wizard.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.context.internal.ManagedSessionContext;
import org.junit.After;
import org.junit.Before;
public abstract class DropwizardDAOTest {
protected abstract SessionFactory getFactory() throws Exception;
private Session session;
@Before
public void setUp() throws Exception
{
session = this.getFactory().openSession();
ManagedSessionContext.bind(session);
}
@After
public void tearDown() throws Exception
{
session.close();
ManagedSessionContext.unbind(getFactory());
}
}
package com.rtr.infra.wizard.test;
import com.google.common.collect.Maps;
import com.yammer.dropwizard.Service;
import net.sourceforge.argparse4j.inf.Namespace;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import java.util.Map;
import com.yammer.dropwizard.bundles.BasicBundle;
import com.yammer.dropwizard.config.Bootstrap;
import com.yammer.dropwizard.config.Configuration;
/**
* JUnit @Rule that'll start and stop a Dropwizard service around each test method.
*
* @author carlo
*/
public class DropwizardTestServer <C extends Configuration, S extends Service<C>> implements TestRule{
private final Class<C> configurationClass;
private final Class<S> serviceClass;
private final String config;
private DropwizardTestServerCommand<C> command;
private S service;
public DropwizardTestServer(Class<C> configClass, Class<S> serviceClass, String config) {
this.configurationClass = configClass;
this.serviceClass = serviceClass;
this.config = config;
}
@Override
public Statement apply(Statement base, Description description) {
return new DropwizardStatement(base);
}
public S getService() {
return service;
}
private class DropwizardStatement extends Statement {
private final Statement base;
private Namespace namespace;
private Bootstrap<C> bootstrap;
public DropwizardStatement(Statement base) {
this.base = base;
}
@Override
public void evaluate() throws Throwable {
service = serviceClass.newInstance();
registerTestCommand(service);
try {
service.initialize(bootstrap);
command.run(bootstrap, namespace);
base.evaluate();
}
finally {
command.stop();
}
}
private void registerTestCommand(Service<C> service) throws Exception {
command = new DropwizardTestServerCommand<C>(service, configurationClass);
bootstrap = new Bootstrap<C>(service);
final Map<String, Object> attrs = Maps.newHashMap();
attrs.put("file", config);
namespace = new Namespace(attrs);
bootstrap.addCommand(command);
bootstrap.addBundle(new BasicBundle());
}
}
}
package com.rtr.infra.wizard.test;
import com.yammer.dropwizard.Service;
import com.yammer.dropwizard.cli.EnvironmentCommand;
import com.yammer.dropwizard.config.Configuration;
import net.sourceforge.argparse4j.inf.Namespace;
import org.eclipse.jetty.server.Server;
import org.jboss.logging.Logger;
import com.yammer.dropwizard.config.Environment;
import com.yammer.dropwizard.config.ServerFactory;
import javax.management.*;
import java.lang.management.ManagementFactory;
import static com.google.common.base.Preconditions.checkArgument;
/**
* Normally ServerCommand is in charge of starting the service, but that's not particularly
* well suited for integration testing as it joins the current thread and keeps the Server
* instance to itself.
*
* This implementation is based on the original ServerCommand, but in addition to being
* stoppable it provides a few convenience methods for tests.
*
* @author Kim A. Betti <kim@developer-b.com> pulled into RTR by Carlo
*/
public class DropwizardTestServerCommand <T extends Configuration> extends EnvironmentCommand<T> {
private final Logger logger = Logger.getLogger(DropwizardTestServerCommand.class);
private final Class<T> configurationClass;
private Server server;
protected DropwizardTestServerCommand(Service<T> service, Class<T> configurationClass) {
super(service, "test-server", "Starts an HTTP test-server running the service");
this.configurationClass = configurationClass;
}
@Override
protected Class<T> getConfigurationClass() {
return configurationClass;
}
@Override
protected void run(Environment environment, Namespace namespace, T configuration) throws Exception {
server = new ServerFactory(configuration.getHttpConfiguration(), environment.getName()).buildServer(environment);
try {
server.start();
environment.serverStarted(server);
} catch (Exception e) {
logger.error("Unable to start server, shutting down", e);
server.stop();
}
}
private void stopJetty() throws Exception {
if (server != null) {
server.stop();
checkArgument(server.isStopped());
}
}
public void stop() throws Exception {
try {
stopJetty();
}
finally {
unRegisterLoggingMBean();
}
}
/**
* We won't be able to run more then a single test in the same JVM instance unless
* we do some tidying and un-register a logging m-bean added by Dropwizard.
*/
private void unRegisterLoggingMBean() throws Exception {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName loggerObjectName = new ObjectName("com.yammer:type=Logging");
if (server.isRegistered(loggerObjectName)) {
server.unregisterMBean(loggerObjectName);
}
}
}
@bennid

This comment has been minimized.

Copy link

@bennid bennid commented Sep 10, 2013

Hi -- I'm new to dropwizard (have been doing javax.persistance / jax-ws with annotations before). How do I go about using these classes for unit tests for my daos? I see that the getFactory() is abstract... but I'd like to kind of have dropwizard wire up everything for me. If there are some examples on how to run these that would be great (and can they run in eclipse as JUnit tests?).

Thanks,
Ben

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment