deltaspike-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rafael Pestano <rmpest...@gmail.com>
Subject Best approach for testing repositories
Date Mon, 06 Apr 2015 20:12:31 GMT
Hi everyone,

besides OpenEJB and cdi-control-ejb used in [1] what are the alternatives
(in terms of Destaspike) for testing JPA based repositories?


For out container integration tests i usually use something like this:

 @Before
  public void inicializarCenario() throws Exception {
    startConnection();
  }

  @After
  public void finalizarCenario() {
    closeConnection();
  }

public void startConnection() {
    emf = Persistence.createEntityManagerFactory("TEST_PU");
    em = emf.createEntityManager();
    em.getTransaction().begin();
  }

  public void closeConnection() {
    em.getTransaction().commit();
    emf.close();
  }

TEST_PU is in test resources and usually uses in memory db, something like
below:

<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

<persistence-unit name="TEST_PU" transaction-type="RESOURCE_LOCAL">
             <provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"
/>
<property name="javax.persistence.jdbc.driver"
value="org.hsqldb.jdbcDriver" />
            <property name="javax.persistence.jdbc.url"
value="jdbc:hsqldb:mem:." />
            <property name="javax.persistence.jdbc.user" value="sa" />
            <property name="javax.persistence.jdbc.password" value="" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.connection.shutdown" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>



The idea is to be "lighweight" and boot only JPA container so my
repositories can use the "test entityManager?

I think of something like this:

@RunWith(CdiTestRunner.class)
@TestControl(startPersistentUnits("TEST_PU"))
public class UserRepositoryTest {

  @Inject
  UserRepository userRepository;

  @Test
  public void shouldInserUser(){
         User u = new User();
 userRepository.save(u);
 assertFalse(user.isTransient());
  }

}



Can we reach somethink like that with Deltaspike test control?

I also would like to integrate with dbunit so I can feed this test
datasource but thats another story

WDYT?



[1]
https://github.com/os890/ee6-ds-demo/blob/master/src/test/java/org/os890/demo/ee6/test/PageBeanTest.java


-- 
<http://www.advancedit.com.br/>Att,

Rafael M. Pestano

Desenvolvedor Java Cia. de Processamento de Dados do Rio Grande do Sul
http://rpestano.wordpress.com/
@realpestano <https://twitter.com/realpestano>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message