Skip to content

Instantly share code, notes, and snippets.

@jwgmeligmeyling
Last active May 27, 2020 16:53
Show Gist options
  • Save jwgmeligmeyling/785e459c4cbaab606ed8 to your computer and use it in GitHub Desktop.
Save jwgmeligmeyling/785e459c4cbaab606ed8 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);
}
}
@nuzayats
Copy link

Hi, thank you for sharing the useful example. it made my test cases pretty clean.

Also I wrote a blog entry based on this example: http://www.nailedtothex.org/roller/kyle/entry/jukito-integration-with-jpa-and

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