Doubts with DAO method

1

I have a question about a generic DAO being used in a testing project with Vraptor.

This dao, works normal in my requests, but when used in a unit test, the same problem occurs when trying to remove:

@Before
public void init(){
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("TEST");
    EntityManager manager = factory.createEntityManager();

    this.dao = new DaoMaster(manager);
}

@Test
public void refusePersistDuplicateDomain(){ 
    Company c1 = new Company("Name", "teste");
    Company c2 = new Company("Name", "teste");

    Assert.assertTrue(this.dao.persist(c1));

    Assert.assertFalse(this.dao.persist(c2));

    this.dao.remove(c1.getId(), Company.class);
}

It generates the following error when removing the object.

java.lang.IllegalArgumentException: attempt to create delete event with null entity
at org.hibernate.event.spi.DeleteEvent.<init>(DeleteEvent.java:48)
at org.hibernate.internal.SessionImpl.delete(SessionImpl.java:896)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.remove(AbstractEntityManagerImpl.java:1214)
at br.com.adapcon.jpro.dao.DaoMaster.remove(DaoMaster.java:96)
at br.com.adapcon.jpro.test.dao.CompanyDaoTest.refusePersistDuplicateDomain(CompanyDaoTest.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

These are the two methods used.

public boolean persist(IEntity o){
    try{
        this.charger();
        if (o.getId() != null && o.getId() > 0) {
            manager.merge(o);
        } else {
            manager.persist(o);
        }
        manager.getTransaction().commit();
        return true;
    }catch(Exception e){
        return false;
    }
}

public boolean remove(Long id, Class<?> objectClass){
    try{
        this.charger();
        Object o = findById(id, objectClass);
        System.out.println("M: "+id);
        manager.remove(o);
        manager.getTransaction().commit();
        return true;
    }catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

private void charger(){
    if(!manager.getTransaction().isActive()){
        manager.getTransaction().begin();
    }
}
    
asked by anonymous 05.12.2016 / 12:37

1 answer

2

Actually, integration tests should not be running on base but with some in-memory database, but the problem might be because you need to refresh entityManager for each persisted entity before deleting, eg:

  

Update the state of the database instance, replacing the   changes made to the entity, if any.

em.refresh(c1);

Example of configuring the in-memory database.

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" version="2.1">
    <persistence-unit name="minhaUnidadePersistencia-test" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <class>br.com.suaApp.SuaEntidade</class>
        ..... demais entidades
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbc.JDBCDriver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:meuDataBase-test" />
            <property name="javax.persistence.jdbc.user" value="sa" />
            <property name="javax.persistence.jdbc.password" value="" />
            <property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
            <property name="hibernate.show_sql" value="true" />
        </properties>
    </persistence-unit>
</persistence>
    
05.12.2016 / 14:12