0
I’m doing some tests with Junit and, to inject dependencies in it with CDI, I adopted Arquillian. But there’s one problem I haven’t figured out how to solve without using the method flush()
of EntityManager
.
Basically, I need to remove an object and check that it is not in the object listing, which I do with the following commands:
class1DAO.remove(1);
assertFalse(class1DAO.getAll().contains(object1FromClass1));
What the method: remove(Class1 class1);
do DAO do is give a: entityManager.remove(class1);
What would be enough to exclude the object. From what I understood of Hibernate.
Already the method getAll()
returns a entityManager.createQuery("from Class1 c", Class1.class).getResultList();
So far so good! But the object deleted a line above, in the test, comes in the method listing getAll()
and my test fails. The only way I found to avoid this is to give a entityManager.flush();
just below entityManager.remove(class1);
But I was already using Transaction Manager and Dependency Injection Control, precisely, to avoid having to do it on hand. Is there any way to solve this, without explicitly using the flush()
?
put your test there !!
– LR10
Class1 class1 = class1DAO.add(new Class1("payment"));
assertTrue(classe1DAO.getAll().contains(class1));
class1DAO.remove(1);
assertFalse(class1DAO.getAll().contains(class1));
– essejoje
The test is basically this one. Add, check, remove and check. But if I don’t give one
flush()
no remove, the test fails.– essejoje
I removed my answer because I made a mistake with what you’re asking. Without the entityManager.flush() to remove it is a problem, as changes in the database are only written after the transaction is confirmed, the flush causes the changes to be written immediately. So when you try to access a list, the reading gives the error because the transaction has not yet been confirmed.
– Edjane