Hi everyone,
I recently tried to make integration tests using
JPA/Hibernate with Junit and Guice. Basically, I inject my EntityManager
with Guice in my DAO and in my Integration test, I simply test the methods
of my DAO. Here is the code for the DAO.
public class PersonDAO implements IPersonDAO{
private EntityManager em;
@Inject
public PersonDAO(EntityManager em) {
this.em = em;
}
@Override
@Transactional
public Person find(int id) {
return em.find(Person.class, id);
}
@Transactional
public Person save(Person person){
em.persist(person);
return person;
}
@Override
public Person update(Person entity) {
return null;
}
@Override
public Person remove(int id) {
return null;
}
}
The entity is very complicated(This is not a production code, it's just for
fun).
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Now that all this is setup, my integration test tries to wrap up all this
using the Google Guice API. Following the guidelines in the google code
<https://code.google.com/p/google-guice/wiki/JPA> website, I have succeeded
in making my first test pass but the problem appears when I try to have two
tests. Here is the code for my integration test.
public class PersonDAOIT {
private PersonDAO personDAO;
@Before
public void setUp() throws Exception {
Injector injector = Guice.createInjector(new
JpaPersistModule("h2-testing"));
injector.getInstance(AppInitializer.class);
personDAO = injector.getInstance(PersonDAO.class);
}
@After
public void tearDown() throws Exception {
personDAO = null;
}
@Test
public void testSave() {
String name = "felix";
Person person = new Person();
person.setName(name);
Person savedPerson = personDAO.save(person);
assertNotNull(personDAO);
assertNotNull(savedPerson);
assertNotNull(savedPerson.getId());
assertEquals(name, savedPerson.getName());
}
@Test
public void testFind() {
String name = "felix";
Person person = new Person();
person.setName(name);
Person savedPerson = personDAO.save(person);
Person foundPerson = personDAO.find(savedPerson.getId());
assertNotNull(foundPerson);
assertEquals(1, foundPerson.getId());
}
}
Since the creation of the Injector is in the @Before section of my test
class, I can create and drop my h2 database and reinject my EntityManager
for every test, this way I get a fresh, untouched database, which is pretty
handy. But, the only problem is that since I have two tests calling the
@Before and @After, google guice sends me that error.
juin 05, 2014 1:29:40 PM com.google.inject.internal.ProxyFactory <init>
AVERTISSEMENT: Method [public java.lang.Object
com.baobab.rose.dal.PersonDAO.find(int)] is synthetic and is being
intercepted by
[com.google.inject.persist.jpa.JpaLocalTxnInterceptor@e874448]. This could
indicate a bug. The method may be intercepted twice, or may not be
intercepted at all.
juin 05, 2014 1:29:40 PM com.google.inject.internal.ProxyFactory <init>
AVERTISSEMENT: Method [public java.lang.Object
com.baobab.rose.dal.PersonDAO.save(java.lang.Object)] is synthetic and is
being intercepted by
[com.google.inject.persist.jpa.JpaLocalTxnInterceptor@e874448]. This could
indicate a bug. The method may be intercepted twice, or may not be
intercepted at all.
I really don't know what that error message means. Any help is appreciated.
Félix Leblanc
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/d/optout.