Skip to content

Instantly share code, notes, and snippets.

@saswata-dutta
Forked from jwgmeligmeyling/DatabaseModule.java
Created March 25, 2020 15:35
Show Gist options
  • Save saswata-dutta/0663b0d5241529e52597b3fab0ccd6a8 to your computer and use it in GitHub Desktop.
Save saswata-dutta/0663b0d5241529e52597b3fab0ccd6a8 to your computer and use it in GitHub Desktop.
Test Guice projects using the JPAPersistModule with Jukito
import javax.persistence.EntityManager;
import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.persist.PersistService;
import com.google.inject.persist.jpa.JpaPersistModule;
public class DatabaseModule extends AbstractModule {
@Override
protected void configure() {
install(new JpaPersistModule("default"));
bind(JPAInitializer.class).asEagerSingleton();
}
@Singleton
public static class JPAInitializer {
@Inject
public JPAInitializer(final PersistService service) {
service.start();
}
}
}

Test Guice projects using the JPAPersistModule with Jukito

This is an example of how to get the JPAPersistModule to work in Jukito test cases, and shows how the PersistService needs to be started on Injector creation to avoid exceptions.

import org.jukito.JukitoRunner;
import org.jukito.UseModules;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.google.inject.Inject;
import static org.junit.Assert.*;
@RunWith(JukitoRunner.class)
@UseModules(DatabaseModule.class)
public class TestDAO {
@Inject
EntityManager entityManager;
@Test
public void test() {
assertNotNull(entityManager);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment