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.
Last active
May 27, 2020 16:53
-
-
Save jwgmeligmeyling/785e459c4cbaab606ed8 to your computer and use it in GitHub Desktop.
Test Guice projects using the JPAPersistModule with Jukito
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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