You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@deltaspike.apache.org by Rafael Pestano <rm...@gmail.com> on 2015/04/06 22:12:31 UTC

Best approach for testing repositories

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>

Re: Best approach for testing repositories

Posted by Rafael Pestano <rm...@gmail.com>.
Hi again,

I have evolved a bit my example and could managed to test UserRepository
from Gerhard ee6-ds-dm, sources are here:
https://github.com/rmpestano/ee6-ds-demo/
<https://github.com/rmpestano/ee6-ds-demo/>

first thing i did was to disable application EntityManagerProduces in unit
tests (remember i want to use my in memory db for tests):

@Exclude(ifProjectStage = ProjectStage.UnitTest.class)
public class EntityManagerProducer {

    @PersistenceContext(unitName = "demoApplicationPU")
    private EntityManager entityManagerProxy;

    @Produces
    public EntityManager exposeDependentEntityManager() {
        return entityManagerProxy;
    }

}


then i've create my producer in test sources:

public class TestEntityManagerProducer  {

  private EntityManager entityManager;

  @Produces
  @ApplicationScoped
  public EntityManager exposeDependentEntityManager()
  {
    entityManager =
Persistence.createEntityManagerFactory("TEST_PU").createEntityManager();
    return entityManager;
  }

  public void closeEntityManager(@Disposes EntityManager entityManager)
  {
    if (entityManager.isOpen())
    {
      entityManager.close();
    }
  }

}


And here is my repository test:

@RunWith(CdiTestRunner.class)
@TestControl(projectStage = ProjectStage.UnitTest.class)
public class UserRepositoryTest {


  @Inject
  UserRepository userRepository;


  @Before
  public void tearUp(){
    User u = new User("testUser","first","last");
    userRepository.save(u);
    assertNotNull(u.getId());
    assertEquals(userRepository.count(), 1);
  }

  @After
  public void tearDown(){

userRepository.getEntityManager().remove(userRepository.findUser("testUser"));
  }


  @Test
  public void shouldFindUser() {
    User user = userRepository.findUser("testUser");
    assertNotNull(user);
    assertEquals(user.getUserName(),"testUser");
  }


}

I have added deltaspike-jpa module to the project to have transaction
support:

@Transactional
public User save(User user) {
    if (user.isTransient()) {
        entityManager.persist(user);
    } else {
        user = entityManager.merge(user);
    }
    return user;
}

without it, i could not insert users during tests (@Before)

here is test persistence.xml:

<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>
      <class>org.os890.demo.ee6.ds.domain.user.User</class>
      <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>


finally the dependencies I've added to project:

      <dependency>
            <groupId>org.apache.deltaspike.modules</groupId>
            <artifactId>deltaspike-jpa-module-api</artifactId>
            <version>${ds.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.deltaspike.modules</groupId>
            <artifactId>deltaspike-jpa-module-impl</artifactId>
            <version>${ds.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.2.18.Final</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>1.8.0.10</version>
            <scope>test</scope>
        </dependency>


Any other advices?

I hope it helps someone ;)

2015-04-06 17:12 GMT-03:00 Rafael Pestano <rm...@gmail.com>:

> 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>
>



-- 
<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>

Re: Best approach for testing repositories

Posted by Rafael Pestano <rm...@gmail.com>.
Hi Thomas, no specific reason, just want to see how far I can go with ds
and know when I need to use a framework or another.
Em 07/04/2015 04:24, "Thomas Hug" <th...@gmail.com> escreveu:

> Hi Rafael
> Any specific reason not to use Arquillian? You can also integrate the
> persistence extension which works already nicely with DBUnit.
>
> On Tue, Apr 7, 2015 at 12:04 AM, Rafael Pestano <rm...@gmail.com>
> wrote:
>
> > Hi again,
> >
> > I have evolved a bit my example and could managed to test UserRepository
> > from Gerhard ee6-ds-dm, sources are here:
> > https://github.com/rmpestano/ee6-ds-demo/
> > <https://github.com/rmpestano/ee6-ds-demo/>
> >
> > first thing i did was to disable application EntityManagerProduces in
> unit
> > tests (remember i want to use my in memory db for tests):
> >
> > @Exclude(ifProjectStage = ProjectStage.UnitTest.class)
> > public class EntityManagerProducer {
> >
> >     @PersistenceContext(unitName = "demoApplicationPU")
> >     private EntityManager entityManagerProxy;
> >
> >     @Produces
> >     public EntityManager exposeDependentEntityManager() {
> >         return entityManagerProxy;
> >     }
> >
> > }
> >
> >
> > then i've create my producer in test sources:
> >
> > public class TestEntityManagerProducer  {
> >
> >   private EntityManager entityManager;
> >
> >   @Produces
> >   @ApplicationScoped
> >   public EntityManager exposeDependentEntityManager()
> >   {
> >     entityManager =
> > Persistence.createEntityManagerFactory("TEST_PU").createEntityManager();
> >     return entityManager;
> >   }
> >
> >   public void closeEntityManager(@Disposes EntityManager entityManager)
> >   {
> >     if (entityManager.isOpen())
> >     {
> >       entityManager.close();
> >     }
> >   }
> >
> > }
> >
> >
> > And here is my repository test:
> >
> > @RunWith(CdiTestRunner.class)
> > @TestControl(projectStage = ProjectStage.UnitTest.class)
> > public class UserRepositoryTest {
> >
> >
> >   @Inject
> >   UserRepository userRepository;
> >
> >
> >   @Before
> >   public void tearUp(){
> >     User u = new User("testUser","first","last");
> >     userRepository.save(u);
> >     assertNotNull(u.getId());
> >     assertEquals(userRepository.count(), 1);
> >   }
> >
> >   @After
> >   public void tearDown(){
> >
> >
> >
> userRepository.getEntityManager().remove(userRepository.findUser("testUser"));
> >   }
> >
> >
> >   @Test
> >   public void shouldFindUser() {
> >     User user = userRepository.findUser("testUser");
> >     assertNotNull(user);
> >     assertEquals(user.getUserName(),"testUser");
> >   }
> >
> >
> > }
> >
> > I have added deltaspike-jpa module to the project to have transaction
> > support:
> >
> > @Transactional
> > public User save(User user) {
> >     if (user.isTransient()) {
> >         entityManager.persist(user);
> >     } else {
> >         user = entityManager.merge(user);
> >     }
> >     return user;
> > }
> >
> > without it, i could not insert users during tests (@Before)
> >
> > here is test persistence.xml:
> >
> > <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>
> >       <class>org.os890.demo.ee6.ds.domain.user.User</class>
> >       <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>
> >
> >
> > finally the dependencies I've added to project:
> >
> >       <dependency>
> >             <groupId>org.apache.deltaspike.modules</groupId>
> >             <artifactId>deltaspike-jpa-module-api</artifactId>
> >             <version>${ds.version}</version>
> >         </dependency>
> >
> >         <dependency>
> >             <groupId>org.apache.deltaspike.modules</groupId>
> >             <artifactId>deltaspike-jpa-module-impl</artifactId>
> >             <version>${ds.version}</version>
> >             <scope>runtime</scope>
> >         </dependency>
> >         <dependency>
> >             <groupId>org.hibernate</groupId>
> >             <artifactId>hibernate-entitymanager</artifactId>
> >             <version>4.2.18.Final</version>
> >             <scope>test</scope>
> >         </dependency>
> >
> >         <dependency>
> >             <groupId>hsqldb</groupId>
> >             <artifactId>hsqldb</artifactId>
> >             <version>1.8.0.10</version>
> >             <scope>test</scope>
> >         </dependency>
> >
> >
> > Any other advices?
> >
> > I hope it helps someone ;)
> >
> > 2015-04-06 17:12 GMT-03:00 Rafael Pestano <rm...@gmail.com>:
> >
> > > 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>
> > >
> >
> >
> >
> > --
> > <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>
> >
>

Re: Best approach for testing repositories

Posted by Karl Kildén <ka...@gmail.com>.
I use test-control because I think Arquillian to hard to use. I think it
could have been improved since last time I tried but then it was very many
dependencies and unclear what to use when you use TomEE. Also I had one
strange exception / problem after another. I had to copy some stuff from
deltaspike source to mock out the various internal deltaspike things (if I
remember correctly it was windowContext related). I also had to add
javaassist but in the end it did not work.

All I want is to boot container so test-control is perfect. For me
Test-Control just works... Also I have no use for the partial deployments
because my business logic does not work this way, It always runs in the
full container and I only use test-control for integration tests so It is
only a pain to define each deployment every time.

Test-Control is two dependencies, two configurations and then one
annotation to use. Optionally more but I rarely need it...


I would be really interested in a good github project with examples for a
tomee project that uses deltaspike-data, deltaspike-jsf and arquillian if
anyone has a repo like this


On 7 April 2015 at 11:42, Thomas Hug <th...@gmail.com> wrote:

> Thnx for the clarification Rafael! Most of DS (including the data module)
> is tested with Arquillian, so can't comment much on a DS test control
> approach...
>
> On Tue, Apr 7, 2015 at 11:30 AM, Rafael Pestano <rm...@gmail.com>
> wrote:
>
> > Hi Thomas, no specific reason, just want to see how far I can go with ds
> > and know when I need to use a framework or another.
> > Em 07/04/2015 04:24, "Thomas Hug" <th...@gmail.com> escreveu:
> >
> > > Hi Rafael
> > > Any specific reason not to use Arquillian? You can also integrate the
> > > persistence extension which works already nicely with DBUnit.
> > >
> > > On Tue, Apr 7, 2015 at 12:04 AM, Rafael Pestano <rm...@gmail.com>
> > > wrote:
> > >
> > > > Hi again,
> > > >
> > > > I have evolved a bit my example and could managed to test
> > UserRepository
> > > > from Gerhard ee6-ds-dm, sources are here:
> > > > https://github.com/rmpestano/ee6-ds-demo/
> > > > <https://github.com/rmpestano/ee6-ds-demo/>
> > > >
> > > > first thing i did was to disable application EntityManagerProduces in
> > > unit
> > > > tests (remember i want to use my in memory db for tests):
> > > >
> > > > @Exclude(ifProjectStage = ProjectStage.UnitTest.class)
> > > > public class EntityManagerProducer {
> > > >
> > > >     @PersistenceContext(unitName = "demoApplicationPU")
> > > >     private EntityManager entityManagerProxy;
> > > >
> > > >     @Produces
> > > >     public EntityManager exposeDependentEntityManager() {
> > > >         return entityManagerProxy;
> > > >     }
> > > >
> > > > }
> > > >
> > > >
> > > > then i've create my producer in test sources:
> > > >
> > > > public class TestEntityManagerProducer  {
> > > >
> > > >   private EntityManager entityManager;
> > > >
> > > >   @Produces
> > > >   @ApplicationScoped
> > > >   public EntityManager exposeDependentEntityManager()
> > > >   {
> > > >     entityManager =
> > > >
> > Persistence.createEntityManagerFactory("TEST_PU").createEntityManager();
> > > >     return entityManager;
> > > >   }
> > > >
> > > >   public void closeEntityManager(@Disposes EntityManager
> entityManager)
> > > >   {
> > > >     if (entityManager.isOpen())
> > > >     {
> > > >       entityManager.close();
> > > >     }
> > > >   }
> > > >
> > > > }
> > > >
> > > >
> > > > And here is my repository test:
> > > >
> > > > @RunWith(CdiTestRunner.class)
> > > > @TestControl(projectStage = ProjectStage.UnitTest.class)
> > > > public class UserRepositoryTest {
> > > >
> > > >
> > > >   @Inject
> > > >   UserRepository userRepository;
> > > >
> > > >
> > > >   @Before
> > > >   public void tearUp(){
> > > >     User u = new User("testUser","first","last");
> > > >     userRepository.save(u);
> > > >     assertNotNull(u.getId());
> > > >     assertEquals(userRepository.count(), 1);
> > > >   }
> > > >
> > > >   @After
> > > >   public void tearDown(){
> > > >
> > > >
> > > >
> > >
> >
> userRepository.getEntityManager().remove(userRepository.findUser("testUser"));
> > > >   }
> > > >
> > > >
> > > >   @Test
> > > >   public void shouldFindUser() {
> > > >     User user = userRepository.findUser("testUser");
> > > >     assertNotNull(user);
> > > >     assertEquals(user.getUserName(),"testUser");
> > > >   }
> > > >
> > > >
> > > > }
> > > >
> > > > I have added deltaspike-jpa module to the project to have transaction
> > > > support:
> > > >
> > > > @Transactional
> > > > public User save(User user) {
> > > >     if (user.isTransient()) {
> > > >         entityManager.persist(user);
> > > >     } else {
> > > >         user = entityManager.merge(user);
> > > >     }
> > > >     return user;
> > > > }
> > > >
> > > > without it, i could not insert users during tests (@Before)
> > > >
> > > > here is test persistence.xml:
> > > >
> > > > <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>
> > > >       <class>org.os890.demo.ee6.ds.domain.user.User</class>
> > > >       <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>
> > > >
> > > >
> > > > finally the dependencies I've added to project:
> > > >
> > > >       <dependency>
> > > >             <groupId>org.apache.deltaspike.modules</groupId>
> > > >             <artifactId>deltaspike-jpa-module-api</artifactId>
> > > >             <version>${ds.version}</version>
> > > >         </dependency>
> > > >
> > > >         <dependency>
> > > >             <groupId>org.apache.deltaspike.modules</groupId>
> > > >             <artifactId>deltaspike-jpa-module-impl</artifactId>
> > > >             <version>${ds.version}</version>
> > > >             <scope>runtime</scope>
> > > >         </dependency>
> > > >         <dependency>
> > > >             <groupId>org.hibernate</groupId>
> > > >             <artifactId>hibernate-entitymanager</artifactId>
> > > >             <version>4.2.18.Final</version>
> > > >             <scope>test</scope>
> > > >         </dependency>
> > > >
> > > >         <dependency>
> > > >             <groupId>hsqldb</groupId>
> > > >             <artifactId>hsqldb</artifactId>
> > > >             <version>1.8.0.10</version>
> > > >             <scope>test</scope>
> > > >         </dependency>
> > > >
> > > >
> > > > Any other advices?
> > > >
> > > > I hope it helps someone ;)
> > > >
> > > > 2015-04-06 17:12 GMT-03:00 Rafael Pestano <rm...@gmail.com>:
> > > >
> > > > > 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>
> > > > >
> > > >
> > > >
> > > >
> > > > --
> > > > <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>
> > > >
> > >
> >
>

Re: Best approach for testing repositories

Posted by Rafael Pestano <rm...@gmail.com>.
Hi again,

when I use Deltaspike Data module I cant insert Users during tests:

Here is tests (the first passes, the recond fails:

@RunWith(CdiTestRunner.class)
@TestControl(projectStage = ProjectStage.UnitTest.class)
public class UserRepositoryDataTest {

  @Inject
  UserRepositoryData userRepositoryData;

  @Inject
  UserRepository userRepository;

  @Test
  public void shouldInserUser() {
    User u = new User("testUser", "first", "last");
    userRepository.save(u);
    assertNotNull(u.getId());
    assertEquals(userRepository.count(), Long.valueOf(1));
  }

  @Test
  public void shouldInserUserUsingDeltaspikeData() {
    User u = new User("testUser", "first", "last");
    userRepositoryData.save(u);
    assertNotNull(u.getId());//null
    assertEquals(userRepositoryData.count(), Long.valueOf(1));
  }

}


UserRepositoryData
<https://github.com/rmpestano/ee6-ds-demo/blob/master/src/main/java/org/os890/demo/ee6/ds/backend/user/UserRepositoryData.java>is
same as UserRepository
<https://github.com/rmpestano/ee6-ds-demo/blob/master/src/main/java/org/os890/demo/ee6/ds/backend/user/UserRepository.java>
except that it *implements *EntityRepository<User, Long>,
CriteriaSupport<User>

test output is here: http://pastebin.com/D1UfwE5P




2015-04-07 11:57 GMT-03:00 Gerhard Petracek <ge...@gmail.com>:

> hi rafael,
>
> you mean the reliability of the tests you can write and not the
> framework/module itself.
> (for arquillian that's also just true if you deploy to a remote container.)
>
> for jpa you don't need an external container. just use a specialized
> producer for the entity-manager and use
> Persistence#createEntityManagerFactory to create an EntityManagerFactory ->
> create an EntityManager with it.
>
> if you need ejb-support as well, you need an adapter which controls the
> embedded ejb-container (like deltaspike-cdictrl-openejb).
>
> regards,
> gerhard
>
> http://www.irian.at
>
> Your JavaEE powerhouse -
> JavaEE Consulting, Development and
> Courses in English and German
>
> Professional Support for Apache
> MyFaces, DeltaSpike and OpenWebBeans
>
>
>
> 2015-04-07 16:38 GMT+02:00 Rafael Pestano <rm...@gmail.com>:
>
> > Hi Gerhard, i can't agree on reliability if i am "emulating" prod
> > environment using embedded containers but thats my opinion and also not
> the
> > idea of this thread (arq x test-control).
> >
> > About the external container, do we have any documentation on how to
> build
> > one?
> >
> > Can I take [1] as reference to create a JPA adapter?
> >
> >
> > [1]
> >
> >
> https://github.com/apache/deltaspike/blob/master/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MyFacesContainerAdapter.java
> >
> >
> >
> >
> > 2015-04-07 11:16 GMT-03:00 Gerhard Petracek <gerhard.petracek@gmail.com
> >:
> >
> > > hi rafael,
> > >
> > > arquillian follows a different idea, but it isn't more reliable.
> > >
> > > the basic idea of test-control is:
> > > test your application as it is. if you need special handling for tests,
> > use
> > > specialized/alternative beans in the test-classpath (and/or @Exclude +
> > > project-stage).
> > > (for sure there are some limitations for web-applications per
> > definition.)
> > >
> > > if you are using deltaspike-cdictrl-openejb (instead of the modules for
> > owb
> > > or weld), you get ejb support as well.
> > > (if you need any other embedded ejb-container which supports the same,
> > you
> > > need to integrate it manually via your own cdictrl-module or via
> > > org.apache.deltaspike.testcontrol.spi.ExternalContainer)
> > >
> > > if you are using weld, you just need to know the bda-rules (and the
> > > global-alternative feature or @javax.annotation.Priority) to use
> > > specialized/alternative beans for your tests.
> > >
> > > regards,
> > > gerhard
> > >
> > > http://www.irian.at
> > >
> > > Your JavaEE powerhouse -
> > > JavaEE Consulting, Development and
> > > Courses in English and German
> > >
> > > Professional Support for Apache
> > > MyFaces, DeltaSpike and OpenWebBeans
> > >
> > >
> > >
> > > 2015-04-07 13:17 GMT+02:00 Rafael Pestano <rm...@gmail.com>:
> > >
> > > > Hi Karl, didnt saw your message.
> > > >
> > > > I think arquillian configuration is not that hard. I can try to add
> it
> > to
> > > > the github repo mentioned in this thread.
> > > > Em 07/04/2015 08:12, "Rafael Pestano" <rm...@gmail.com>
> escreveu:
> > > >
> > > > > I see,
> > > > >
> > > > > In simple words I think test control would give more speed and
> > > arquillian
> > > > > more reliabie tests.
> > > > >
> > > > > So if we got transaction support and data store management in test
> > > > control
> > > > > I think the module would receive more adoption.
> > > > > Em 07/04/2015 06:43, "Thomas Hug" <th...@gmail.com> escreveu:
> > > > >
> > > > >> Thnx for the clarification Rafael! Most of DS (including the data
> > > > module)
> > > > >> is tested with Arquillian, so can't comment much on a DS test
> > control
> > > > >> approach...
> > > > >>
> > > > >> On Tue, Apr 7, 2015 at 11:30 AM, Rafael Pestano <
> > rmpestano@gmail.com>
> > > > >> wrote:
> > > > >>
> > > > >> > Hi Thomas, no specific reason, just want to see how far I can go
> > > with
> > > > ds
> > > > >> > and know when I need to use a framework or another.
> > > > >> > Em 07/04/2015 04:24, "Thomas Hug" <th...@gmail.com>
> > escreveu:
> > > > >> >
> > > > >> > > Hi Rafael
> > > > >> > > Any specific reason not to use Arquillian? You can also
> > integrate
> > > > the
> > > > >> > > persistence extension which works already nicely with DBUnit.
> > > > >> > >
> > > > >> > > On Tue, Apr 7, 2015 at 12:04 AM, Rafael Pestano <
> > > > rmpestano@gmail.com>
> > > > >> > > wrote:
> > > > >> > >
> > > > >> > > > Hi again,
> > > > >> > > >
> > > > >> > > > I have evolved a bit my example and could managed to test
> > > > >> > UserRepository
> > > > >> > > > from Gerhard ee6-ds-dm, sources are here:
> > > > >> > > > https://github.com/rmpestano/ee6-ds-demo/
> > > > >> > > > <https://github.com/rmpestano/ee6-ds-demo/>
> > > > >> > > >
> > > > >> > > > first thing i did was to disable application
> > > EntityManagerProduces
> > > > >> in
> > > > >> > > unit
> > > > >> > > > tests (remember i want to use my in memory db for tests):
> > > > >> > > >
> > > > >> > > > @Exclude(ifProjectStage = ProjectStage.UnitTest.class)
> > > > >> > > > public class EntityManagerProducer {
> > > > >> > > >
> > > > >> > > >     @PersistenceContext(unitName = "demoApplicationPU")
> > > > >> > > >     private EntityManager entityManagerProxy;
> > > > >> > > >
> > > > >> > > >     @Produces
> > > > >> > > >     public EntityManager exposeDependentEntityManager() {
> > > > >> > > >         return entityManagerProxy;
> > > > >> > > >     }
> > > > >> > > >
> > > > >> > > > }
> > > > >> > > >
> > > > >> > > >
> > > > >> > > > then i've create my producer in test sources:
> > > > >> > > >
> > > > >> > > > public class TestEntityManagerProducer  {
> > > > >> > > >
> > > > >> > > >   private EntityManager entityManager;
> > > > >> > > >
> > > > >> > > >   @Produces
> > > > >> > > >   @ApplicationScoped
> > > > >> > > >   public EntityManager exposeDependentEntityManager()
> > > > >> > > >   {
> > > > >> > > >     entityManager =
> > > > >> > > >
> > > > >> >
> > > >
> > Persistence.createEntityManagerFactory("TEST_PU").createEntityManager();
> > > > >> > > >     return entityManager;
> > > > >> > > >   }
> > > > >> > > >
> > > > >> > > >   public void closeEntityManager(@Disposes EntityManager
> > > > >> entityManager)
> > > > >> > > >   {
> > > > >> > > >     if (entityManager.isOpen())
> > > > >> > > >     {
> > > > >> > > >       entityManager.close();
> > > > >> > > >     }
> > > > >> > > >   }
> > > > >> > > >
> > > > >> > > > }
> > > > >> > > >
> > > > >> > > >
> > > > >> > > > And here is my repository test:
> > > > >> > > >
> > > > >> > > > @RunWith(CdiTestRunner.class)
> > > > >> > > > @TestControl(projectStage = ProjectStage.UnitTest.class)
> > > > >> > > > public class UserRepositoryTest {
> > > > >> > > >
> > > > >> > > >
> > > > >> > > >   @Inject
> > > > >> > > >   UserRepository userRepository;
> > > > >> > > >
> > > > >> > > >
> > > > >> > > >   @Before
> > > > >> > > >   public void tearUp(){
> > > > >> > > >     User u = new User("testUser","first","last");
> > > > >> > > >     userRepository.save(u);
> > > > >> > > >     assertNotNull(u.getId());
> > > > >> > > >     assertEquals(userRepository.count(), 1);
> > > > >> > > >   }
> > > > >> > > >
> > > > >> > > >   @After
> > > > >> > > >   public void tearDown(){
> > > > >> > > >
> > > > >> > > >
> > > > >> > > >
> > > > >> > >
> > > > >> >
> > > > >>
> > > >
> > >
> >
> userRepository.getEntityManager().remove(userRepository.findUser("testUser"));
> > > > >> > > >   }
> > > > >> > > >
> > > > >> > > >
> > > > >> > > >   @Test
> > > > >> > > >   public void shouldFindUser() {
> > > > >> > > >     User user = userRepository.findUser("testUser");
> > > > >> > > >     assertNotNull(user);
> > > > >> > > >     assertEquals(user.getUserName(),"testUser");
> > > > >> > > >   }
> > > > >> > > >
> > > > >> > > >
> > > > >> > > > }
> > > > >> > > >
> > > > >> > > > I have added deltaspike-jpa module to the project to have
> > > > >> transaction
> > > > >> > > > support:
> > > > >> > > >
> > > > >> > > > @Transactional
> > > > >> > > > public User save(User user) {
> > > > >> > > >     if (user.isTransient()) {
> > > > >> > > >         entityManager.persist(user);
> > > > >> > > >     } else {
> > > > >> > > >         user = entityManager.merge(user);
> > > > >> > > >     }
> > > > >> > > >     return user;
> > > > >> > > > }
> > > > >> > > >
> > > > >> > > > without it, i could not insert users during tests (@Before)
> > > > >> > > >
> > > > >> > > > here is test persistence.xml:
> > > > >> > > >
> > > > >> > > > <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>
> > > > >> > > >       <class>org.os890.demo.ee6.ds.domain.user.User</class>
> > > > >> > > >       <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>
> > > > >> > > >
> > > > >> > > >
> > > > >> > > > finally the dependencies I've added to project:
> > > > >> > > >
> > > > >> > > >       <dependency>
> > > > >> > > >             <groupId>org.apache.deltaspike.modules</groupId>
> > > > >> > > >
>  <artifactId>deltaspike-jpa-module-api</artifactId>
> > > > >> > > >             <version>${ds.version}</version>
> > > > >> > > >         </dependency>
> > > > >> > > >
> > > > >> > > >         <dependency>
> > > > >> > > >             <groupId>org.apache.deltaspike.modules</groupId>
> > > > >> > > >
> >  <artifactId>deltaspike-jpa-module-impl</artifactId>
> > > > >> > > >             <version>${ds.version}</version>
> > > > >> > > >             <scope>runtime</scope>
> > > > >> > > >         </dependency>
> > > > >> > > >         <dependency>
> > > > >> > > >             <groupId>org.hibernate</groupId>
> > > > >> > > >             <artifactId>hibernate-entitymanager</artifactId>
> > > > >> > > >             <version>4.2.18.Final</version>
> > > > >> > > >             <scope>test</scope>
> > > > >> > > >         </dependency>
> > > > >> > > >
> > > > >> > > >         <dependency>
> > > > >> > > >             <groupId>hsqldb</groupId>
> > > > >> > > >             <artifactId>hsqldb</artifactId>
> > > > >> > > >             <version>1.8.0.10</version>
> > > > >> > > >             <scope>test</scope>
> > > > >> > > >         </dependency>
> > > > >> > > >
> > > > >> > > >
> > > > >> > > > Any other advices?
> > > > >> > > >
> > > > >> > > > I hope it helps someone ;)
> > > > >> > > >
> > > > >> > > > 2015-04-06 17:12 GMT-03:00 Rafael Pestano <
> > rmpestano@gmail.com
> > > >:
> > > > >> > > >
> > > > >> > > > > 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>
> > > > >> > > > >
> > > > >> > > >
> > > > >> > > >
> > > > >> > > >
> > > > >> > > > --
> > > > >> > > > <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>
> > > > >> > > >
> > > > >> > >
> > > > >> >
> > > > >>
> > > > >
> > > >
> > >
> >
> >
> >
> > --
> > <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>
> >
>



-- 
<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>

Re: Best approach for testing repositories

Posted by Gerhard Petracek <ge...@gmail.com>.
hi rafael,

you mean the reliability of the tests you can write and not the
framework/module itself.
(for arquillian that's also just true if you deploy to a remote container.)

for jpa you don't need an external container. just use a specialized
producer for the entity-manager and use
Persistence#createEntityManagerFactory to create an EntityManagerFactory ->
create an EntityManager with it.

if you need ejb-support as well, you need an adapter which controls the
embedded ejb-container (like deltaspike-cdictrl-openejb).

regards,
gerhard

http://www.irian.at

Your JavaEE powerhouse -
JavaEE Consulting, Development and
Courses in English and German

Professional Support for Apache
MyFaces, DeltaSpike and OpenWebBeans



2015-04-07 16:38 GMT+02:00 Rafael Pestano <rm...@gmail.com>:

> Hi Gerhard, i can't agree on reliability if i am "emulating" prod
> environment using embedded containers but thats my opinion and also not the
> idea of this thread (arq x test-control).
>
> About the external container, do we have any documentation on how to build
> one?
>
> Can I take [1] as reference to create a JPA adapter?
>
>
> [1]
>
> https://github.com/apache/deltaspike/blob/master/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MyFacesContainerAdapter.java
>
>
>
>
> 2015-04-07 11:16 GMT-03:00 Gerhard Petracek <ge...@gmail.com>:
>
> > hi rafael,
> >
> > arquillian follows a different idea, but it isn't more reliable.
> >
> > the basic idea of test-control is:
> > test your application as it is. if you need special handling for tests,
> use
> > specialized/alternative beans in the test-classpath (and/or @Exclude +
> > project-stage).
> > (for sure there are some limitations for web-applications per
> definition.)
> >
> > if you are using deltaspike-cdictrl-openejb (instead of the modules for
> owb
> > or weld), you get ejb support as well.
> > (if you need any other embedded ejb-container which supports the same,
> you
> > need to integrate it manually via your own cdictrl-module or via
> > org.apache.deltaspike.testcontrol.spi.ExternalContainer)
> >
> > if you are using weld, you just need to know the bda-rules (and the
> > global-alternative feature or @javax.annotation.Priority) to use
> > specialized/alternative beans for your tests.
> >
> > regards,
> > gerhard
> >
> > http://www.irian.at
> >
> > Your JavaEE powerhouse -
> > JavaEE Consulting, Development and
> > Courses in English and German
> >
> > Professional Support for Apache
> > MyFaces, DeltaSpike and OpenWebBeans
> >
> >
> >
> > 2015-04-07 13:17 GMT+02:00 Rafael Pestano <rm...@gmail.com>:
> >
> > > Hi Karl, didnt saw your message.
> > >
> > > I think arquillian configuration is not that hard. I can try to add it
> to
> > > the github repo mentioned in this thread.
> > > Em 07/04/2015 08:12, "Rafael Pestano" <rm...@gmail.com> escreveu:
> > >
> > > > I see,
> > > >
> > > > In simple words I think test control would give more speed and
> > arquillian
> > > > more reliabie tests.
> > > >
> > > > So if we got transaction support and data store management in test
> > > control
> > > > I think the module would receive more adoption.
> > > > Em 07/04/2015 06:43, "Thomas Hug" <th...@gmail.com> escreveu:
> > > >
> > > >> Thnx for the clarification Rafael! Most of DS (including the data
> > > module)
> > > >> is tested with Arquillian, so can't comment much on a DS test
> control
> > > >> approach...
> > > >>
> > > >> On Tue, Apr 7, 2015 at 11:30 AM, Rafael Pestano <
> rmpestano@gmail.com>
> > > >> wrote:
> > > >>
> > > >> > Hi Thomas, no specific reason, just want to see how far I can go
> > with
> > > ds
> > > >> > and know when I need to use a framework or another.
> > > >> > Em 07/04/2015 04:24, "Thomas Hug" <th...@gmail.com>
> escreveu:
> > > >> >
> > > >> > > Hi Rafael
> > > >> > > Any specific reason not to use Arquillian? You can also
> integrate
> > > the
> > > >> > > persistence extension which works already nicely with DBUnit.
> > > >> > >
> > > >> > > On Tue, Apr 7, 2015 at 12:04 AM, Rafael Pestano <
> > > rmpestano@gmail.com>
> > > >> > > wrote:
> > > >> > >
> > > >> > > > Hi again,
> > > >> > > >
> > > >> > > > I have evolved a bit my example and could managed to test
> > > >> > UserRepository
> > > >> > > > from Gerhard ee6-ds-dm, sources are here:
> > > >> > > > https://github.com/rmpestano/ee6-ds-demo/
> > > >> > > > <https://github.com/rmpestano/ee6-ds-demo/>
> > > >> > > >
> > > >> > > > first thing i did was to disable application
> > EntityManagerProduces
> > > >> in
> > > >> > > unit
> > > >> > > > tests (remember i want to use my in memory db for tests):
> > > >> > > >
> > > >> > > > @Exclude(ifProjectStage = ProjectStage.UnitTest.class)
> > > >> > > > public class EntityManagerProducer {
> > > >> > > >
> > > >> > > >     @PersistenceContext(unitName = "demoApplicationPU")
> > > >> > > >     private EntityManager entityManagerProxy;
> > > >> > > >
> > > >> > > >     @Produces
> > > >> > > >     public EntityManager exposeDependentEntityManager() {
> > > >> > > >         return entityManagerProxy;
> > > >> > > >     }
> > > >> > > >
> > > >> > > > }
> > > >> > > >
> > > >> > > >
> > > >> > > > then i've create my producer in test sources:
> > > >> > > >
> > > >> > > > public class TestEntityManagerProducer  {
> > > >> > > >
> > > >> > > >   private EntityManager entityManager;
> > > >> > > >
> > > >> > > >   @Produces
> > > >> > > >   @ApplicationScoped
> > > >> > > >   public EntityManager exposeDependentEntityManager()
> > > >> > > >   {
> > > >> > > >     entityManager =
> > > >> > > >
> > > >> >
> > >
> Persistence.createEntityManagerFactory("TEST_PU").createEntityManager();
> > > >> > > >     return entityManager;
> > > >> > > >   }
> > > >> > > >
> > > >> > > >   public void closeEntityManager(@Disposes EntityManager
> > > >> entityManager)
> > > >> > > >   {
> > > >> > > >     if (entityManager.isOpen())
> > > >> > > >     {
> > > >> > > >       entityManager.close();
> > > >> > > >     }
> > > >> > > >   }
> > > >> > > >
> > > >> > > > }
> > > >> > > >
> > > >> > > >
> > > >> > > > And here is my repository test:
> > > >> > > >
> > > >> > > > @RunWith(CdiTestRunner.class)
> > > >> > > > @TestControl(projectStage = ProjectStage.UnitTest.class)
> > > >> > > > public class UserRepositoryTest {
> > > >> > > >
> > > >> > > >
> > > >> > > >   @Inject
> > > >> > > >   UserRepository userRepository;
> > > >> > > >
> > > >> > > >
> > > >> > > >   @Before
> > > >> > > >   public void tearUp(){
> > > >> > > >     User u = new User("testUser","first","last");
> > > >> > > >     userRepository.save(u);
> > > >> > > >     assertNotNull(u.getId());
> > > >> > > >     assertEquals(userRepository.count(), 1);
> > > >> > > >   }
> > > >> > > >
> > > >> > > >   @After
> > > >> > > >   public void tearDown(){
> > > >> > > >
> > > >> > > >
> > > >> > > >
> > > >> > >
> > > >> >
> > > >>
> > >
> >
> userRepository.getEntityManager().remove(userRepository.findUser("testUser"));
> > > >> > > >   }
> > > >> > > >
> > > >> > > >
> > > >> > > >   @Test
> > > >> > > >   public void shouldFindUser() {
> > > >> > > >     User user = userRepository.findUser("testUser");
> > > >> > > >     assertNotNull(user);
> > > >> > > >     assertEquals(user.getUserName(),"testUser");
> > > >> > > >   }
> > > >> > > >
> > > >> > > >
> > > >> > > > }
> > > >> > > >
> > > >> > > > I have added deltaspike-jpa module to the project to have
> > > >> transaction
> > > >> > > > support:
> > > >> > > >
> > > >> > > > @Transactional
> > > >> > > > public User save(User user) {
> > > >> > > >     if (user.isTransient()) {
> > > >> > > >         entityManager.persist(user);
> > > >> > > >     } else {
> > > >> > > >         user = entityManager.merge(user);
> > > >> > > >     }
> > > >> > > >     return user;
> > > >> > > > }
> > > >> > > >
> > > >> > > > without it, i could not insert users during tests (@Before)
> > > >> > > >
> > > >> > > > here is test persistence.xml:
> > > >> > > >
> > > >> > > > <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>
> > > >> > > >       <class>org.os890.demo.ee6.ds.domain.user.User</class>
> > > >> > > >       <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>
> > > >> > > >
> > > >> > > >
> > > >> > > > finally the dependencies I've added to project:
> > > >> > > >
> > > >> > > >       <dependency>
> > > >> > > >             <groupId>org.apache.deltaspike.modules</groupId>
> > > >> > > >             <artifactId>deltaspike-jpa-module-api</artifactId>
> > > >> > > >             <version>${ds.version}</version>
> > > >> > > >         </dependency>
> > > >> > > >
> > > >> > > >         <dependency>
> > > >> > > >             <groupId>org.apache.deltaspike.modules</groupId>
> > > >> > > >
>  <artifactId>deltaspike-jpa-module-impl</artifactId>
> > > >> > > >             <version>${ds.version}</version>
> > > >> > > >             <scope>runtime</scope>
> > > >> > > >         </dependency>
> > > >> > > >         <dependency>
> > > >> > > >             <groupId>org.hibernate</groupId>
> > > >> > > >             <artifactId>hibernate-entitymanager</artifactId>
> > > >> > > >             <version>4.2.18.Final</version>
> > > >> > > >             <scope>test</scope>
> > > >> > > >         </dependency>
> > > >> > > >
> > > >> > > >         <dependency>
> > > >> > > >             <groupId>hsqldb</groupId>
> > > >> > > >             <artifactId>hsqldb</artifactId>
> > > >> > > >             <version>1.8.0.10</version>
> > > >> > > >             <scope>test</scope>
> > > >> > > >         </dependency>
> > > >> > > >
> > > >> > > >
> > > >> > > > Any other advices?
> > > >> > > >
> > > >> > > > I hope it helps someone ;)
> > > >> > > >
> > > >> > > > 2015-04-06 17:12 GMT-03:00 Rafael Pestano <
> rmpestano@gmail.com
> > >:
> > > >> > > >
> > > >> > > > > 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>
> > > >> > > > >
> > > >> > > >
> > > >> > > >
> > > >> > > >
> > > >> > > > --
> > > >> > > > <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>
> > > >> > > >
> > > >> > >
> > > >> >
> > > >>
> > > >
> > >
> >
>
>
>
> --
> <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>
>

Re: Best approach for testing repositories

Posted by Rafael Pestano <rm...@gmail.com>.
Hi Gerhard, i can't agree on reliability if i am "emulating" prod
environment using embedded containers but thats my opinion and also not the
idea of this thread (arq x test-control).

About the external container, do we have any documentation on how to build
one?

Can I take [1] as reference to create a JPA adapter?


[1]
https://github.com/apache/deltaspike/blob/master/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MyFacesContainerAdapter.java




2015-04-07 11:16 GMT-03:00 Gerhard Petracek <ge...@gmail.com>:

> hi rafael,
>
> arquillian follows a different idea, but it isn't more reliable.
>
> the basic idea of test-control is:
> test your application as it is. if you need special handling for tests, use
> specialized/alternative beans in the test-classpath (and/or @Exclude +
> project-stage).
> (for sure there are some limitations for web-applications per definition.)
>
> if you are using deltaspike-cdictrl-openejb (instead of the modules for owb
> or weld), you get ejb support as well.
> (if you need any other embedded ejb-container which supports the same, you
> need to integrate it manually via your own cdictrl-module or via
> org.apache.deltaspike.testcontrol.spi.ExternalContainer)
>
> if you are using weld, you just need to know the bda-rules (and the
> global-alternative feature or @javax.annotation.Priority) to use
> specialized/alternative beans for your tests.
>
> regards,
> gerhard
>
> http://www.irian.at
>
> Your JavaEE powerhouse -
> JavaEE Consulting, Development and
> Courses in English and German
>
> Professional Support for Apache
> MyFaces, DeltaSpike and OpenWebBeans
>
>
>
> 2015-04-07 13:17 GMT+02:00 Rafael Pestano <rm...@gmail.com>:
>
> > Hi Karl, didnt saw your message.
> >
> > I think arquillian configuration is not that hard. I can try to add it to
> > the github repo mentioned in this thread.
> > Em 07/04/2015 08:12, "Rafael Pestano" <rm...@gmail.com> escreveu:
> >
> > > I see,
> > >
> > > In simple words I think test control would give more speed and
> arquillian
> > > more reliabie tests.
> > >
> > > So if we got transaction support and data store management in test
> > control
> > > I think the module would receive more adoption.
> > > Em 07/04/2015 06:43, "Thomas Hug" <th...@gmail.com> escreveu:
> > >
> > >> Thnx for the clarification Rafael! Most of DS (including the data
> > module)
> > >> is tested with Arquillian, so can't comment much on a DS test control
> > >> approach...
> > >>
> > >> On Tue, Apr 7, 2015 at 11:30 AM, Rafael Pestano <rm...@gmail.com>
> > >> wrote:
> > >>
> > >> > Hi Thomas, no specific reason, just want to see how far I can go
> with
> > ds
> > >> > and know when I need to use a framework or another.
> > >> > Em 07/04/2015 04:24, "Thomas Hug" <th...@gmail.com> escreveu:
> > >> >
> > >> > > Hi Rafael
> > >> > > Any specific reason not to use Arquillian? You can also integrate
> > the
> > >> > > persistence extension which works already nicely with DBUnit.
> > >> > >
> > >> > > On Tue, Apr 7, 2015 at 12:04 AM, Rafael Pestano <
> > rmpestano@gmail.com>
> > >> > > wrote:
> > >> > >
> > >> > > > Hi again,
> > >> > > >
> > >> > > > I have evolved a bit my example and could managed to test
> > >> > UserRepository
> > >> > > > from Gerhard ee6-ds-dm, sources are here:
> > >> > > > https://github.com/rmpestano/ee6-ds-demo/
> > >> > > > <https://github.com/rmpestano/ee6-ds-demo/>
> > >> > > >
> > >> > > > first thing i did was to disable application
> EntityManagerProduces
> > >> in
> > >> > > unit
> > >> > > > tests (remember i want to use my in memory db for tests):
> > >> > > >
> > >> > > > @Exclude(ifProjectStage = ProjectStage.UnitTest.class)
> > >> > > > public class EntityManagerProducer {
> > >> > > >
> > >> > > >     @PersistenceContext(unitName = "demoApplicationPU")
> > >> > > >     private EntityManager entityManagerProxy;
> > >> > > >
> > >> > > >     @Produces
> > >> > > >     public EntityManager exposeDependentEntityManager() {
> > >> > > >         return entityManagerProxy;
> > >> > > >     }
> > >> > > >
> > >> > > > }
> > >> > > >
> > >> > > >
> > >> > > > then i've create my producer in test sources:
> > >> > > >
> > >> > > > public class TestEntityManagerProducer  {
> > >> > > >
> > >> > > >   private EntityManager entityManager;
> > >> > > >
> > >> > > >   @Produces
> > >> > > >   @ApplicationScoped
> > >> > > >   public EntityManager exposeDependentEntityManager()
> > >> > > >   {
> > >> > > >     entityManager =
> > >> > > >
> > >> >
> > Persistence.createEntityManagerFactory("TEST_PU").createEntityManager();
> > >> > > >     return entityManager;
> > >> > > >   }
> > >> > > >
> > >> > > >   public void closeEntityManager(@Disposes EntityManager
> > >> entityManager)
> > >> > > >   {
> > >> > > >     if (entityManager.isOpen())
> > >> > > >     {
> > >> > > >       entityManager.close();
> > >> > > >     }
> > >> > > >   }
> > >> > > >
> > >> > > > }
> > >> > > >
> > >> > > >
> > >> > > > And here is my repository test:
> > >> > > >
> > >> > > > @RunWith(CdiTestRunner.class)
> > >> > > > @TestControl(projectStage = ProjectStage.UnitTest.class)
> > >> > > > public class UserRepositoryTest {
> > >> > > >
> > >> > > >
> > >> > > >   @Inject
> > >> > > >   UserRepository userRepository;
> > >> > > >
> > >> > > >
> > >> > > >   @Before
> > >> > > >   public void tearUp(){
> > >> > > >     User u = new User("testUser","first","last");
> > >> > > >     userRepository.save(u);
> > >> > > >     assertNotNull(u.getId());
> > >> > > >     assertEquals(userRepository.count(), 1);
> > >> > > >   }
> > >> > > >
> > >> > > >   @After
> > >> > > >   public void tearDown(){
> > >> > > >
> > >> > > >
> > >> > > >
> > >> > >
> > >> >
> > >>
> >
> userRepository.getEntityManager().remove(userRepository.findUser("testUser"));
> > >> > > >   }
> > >> > > >
> > >> > > >
> > >> > > >   @Test
> > >> > > >   public void shouldFindUser() {
> > >> > > >     User user = userRepository.findUser("testUser");
> > >> > > >     assertNotNull(user);
> > >> > > >     assertEquals(user.getUserName(),"testUser");
> > >> > > >   }
> > >> > > >
> > >> > > >
> > >> > > > }
> > >> > > >
> > >> > > > I have added deltaspike-jpa module to the project to have
> > >> transaction
> > >> > > > support:
> > >> > > >
> > >> > > > @Transactional
> > >> > > > public User save(User user) {
> > >> > > >     if (user.isTransient()) {
> > >> > > >         entityManager.persist(user);
> > >> > > >     } else {
> > >> > > >         user = entityManager.merge(user);
> > >> > > >     }
> > >> > > >     return user;
> > >> > > > }
> > >> > > >
> > >> > > > without it, i could not insert users during tests (@Before)
> > >> > > >
> > >> > > > here is test persistence.xml:
> > >> > > >
> > >> > > > <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>
> > >> > > >       <class>org.os890.demo.ee6.ds.domain.user.User</class>
> > >> > > >       <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>
> > >> > > >
> > >> > > >
> > >> > > > finally the dependencies I've added to project:
> > >> > > >
> > >> > > >       <dependency>
> > >> > > >             <groupId>org.apache.deltaspike.modules</groupId>
> > >> > > >             <artifactId>deltaspike-jpa-module-api</artifactId>
> > >> > > >             <version>${ds.version}</version>
> > >> > > >         </dependency>
> > >> > > >
> > >> > > >         <dependency>
> > >> > > >             <groupId>org.apache.deltaspike.modules</groupId>
> > >> > > >             <artifactId>deltaspike-jpa-module-impl</artifactId>
> > >> > > >             <version>${ds.version}</version>
> > >> > > >             <scope>runtime</scope>
> > >> > > >         </dependency>
> > >> > > >         <dependency>
> > >> > > >             <groupId>org.hibernate</groupId>
> > >> > > >             <artifactId>hibernate-entitymanager</artifactId>
> > >> > > >             <version>4.2.18.Final</version>
> > >> > > >             <scope>test</scope>
> > >> > > >         </dependency>
> > >> > > >
> > >> > > >         <dependency>
> > >> > > >             <groupId>hsqldb</groupId>
> > >> > > >             <artifactId>hsqldb</artifactId>
> > >> > > >             <version>1.8.0.10</version>
> > >> > > >             <scope>test</scope>
> > >> > > >         </dependency>
> > >> > > >
> > >> > > >
> > >> > > > Any other advices?
> > >> > > >
> > >> > > > I hope it helps someone ;)
> > >> > > >
> > >> > > > 2015-04-06 17:12 GMT-03:00 Rafael Pestano <rmpestano@gmail.com
> >:
> > >> > > >
> > >> > > > > 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>
> > >> > > > >
> > >> > > >
> > >> > > >
> > >> > > >
> > >> > > > --
> > >> > > > <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>
> > >> > > >
> > >> > >
> > >> >
> > >>
> > >
> >
>



-- 
<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>

Re: Best approach for testing repositories

Posted by Gerhard Petracek <ge...@gmail.com>.
hi rafael,

arquillian follows a different idea, but it isn't more reliable.

the basic idea of test-control is:
test your application as it is. if you need special handling for tests, use
specialized/alternative beans in the test-classpath (and/or @Exclude +
project-stage).
(for sure there are some limitations for web-applications per definition.)

if you are using deltaspike-cdictrl-openejb (instead of the modules for owb
or weld), you get ejb support as well.
(if you need any other embedded ejb-container which supports the same, you
need to integrate it manually via your own cdictrl-module or via
org.apache.deltaspike.testcontrol.spi.ExternalContainer)

if you are using weld, you just need to know the bda-rules (and the
global-alternative feature or @javax.annotation.Priority) to use
specialized/alternative beans for your tests.

regards,
gerhard

http://www.irian.at

Your JavaEE powerhouse -
JavaEE Consulting, Development and
Courses in English and German

Professional Support for Apache
MyFaces, DeltaSpike and OpenWebBeans



2015-04-07 13:17 GMT+02:00 Rafael Pestano <rm...@gmail.com>:

> Hi Karl, didnt saw your message.
>
> I think arquillian configuration is not that hard. I can try to add it to
> the github repo mentioned in this thread.
> Em 07/04/2015 08:12, "Rafael Pestano" <rm...@gmail.com> escreveu:
>
> > I see,
> >
> > In simple words I think test control would give more speed and arquillian
> > more reliabie tests.
> >
> > So if we got transaction support and data store management in test
> control
> > I think the module would receive more adoption.
> > Em 07/04/2015 06:43, "Thomas Hug" <th...@gmail.com> escreveu:
> >
> >> Thnx for the clarification Rafael! Most of DS (including the data
> module)
> >> is tested with Arquillian, so can't comment much on a DS test control
> >> approach...
> >>
> >> On Tue, Apr 7, 2015 at 11:30 AM, Rafael Pestano <rm...@gmail.com>
> >> wrote:
> >>
> >> > Hi Thomas, no specific reason, just want to see how far I can go with
> ds
> >> > and know when I need to use a framework or another.
> >> > Em 07/04/2015 04:24, "Thomas Hug" <th...@gmail.com> escreveu:
> >> >
> >> > > Hi Rafael
> >> > > Any specific reason not to use Arquillian? You can also integrate
> the
> >> > > persistence extension which works already nicely with DBUnit.
> >> > >
> >> > > On Tue, Apr 7, 2015 at 12:04 AM, Rafael Pestano <
> rmpestano@gmail.com>
> >> > > wrote:
> >> > >
> >> > > > Hi again,
> >> > > >
> >> > > > I have evolved a bit my example and could managed to test
> >> > UserRepository
> >> > > > from Gerhard ee6-ds-dm, sources are here:
> >> > > > https://github.com/rmpestano/ee6-ds-demo/
> >> > > > <https://github.com/rmpestano/ee6-ds-demo/>
> >> > > >
> >> > > > first thing i did was to disable application EntityManagerProduces
> >> in
> >> > > unit
> >> > > > tests (remember i want to use my in memory db for tests):
> >> > > >
> >> > > > @Exclude(ifProjectStage = ProjectStage.UnitTest.class)
> >> > > > public class EntityManagerProducer {
> >> > > >
> >> > > >     @PersistenceContext(unitName = "demoApplicationPU")
> >> > > >     private EntityManager entityManagerProxy;
> >> > > >
> >> > > >     @Produces
> >> > > >     public EntityManager exposeDependentEntityManager() {
> >> > > >         return entityManagerProxy;
> >> > > >     }
> >> > > >
> >> > > > }
> >> > > >
> >> > > >
> >> > > > then i've create my producer in test sources:
> >> > > >
> >> > > > public class TestEntityManagerProducer  {
> >> > > >
> >> > > >   private EntityManager entityManager;
> >> > > >
> >> > > >   @Produces
> >> > > >   @ApplicationScoped
> >> > > >   public EntityManager exposeDependentEntityManager()
> >> > > >   {
> >> > > >     entityManager =
> >> > > >
> >> >
> Persistence.createEntityManagerFactory("TEST_PU").createEntityManager();
> >> > > >     return entityManager;
> >> > > >   }
> >> > > >
> >> > > >   public void closeEntityManager(@Disposes EntityManager
> >> entityManager)
> >> > > >   {
> >> > > >     if (entityManager.isOpen())
> >> > > >     {
> >> > > >       entityManager.close();
> >> > > >     }
> >> > > >   }
> >> > > >
> >> > > > }
> >> > > >
> >> > > >
> >> > > > And here is my repository test:
> >> > > >
> >> > > > @RunWith(CdiTestRunner.class)
> >> > > > @TestControl(projectStage = ProjectStage.UnitTest.class)
> >> > > > public class UserRepositoryTest {
> >> > > >
> >> > > >
> >> > > >   @Inject
> >> > > >   UserRepository userRepository;
> >> > > >
> >> > > >
> >> > > >   @Before
> >> > > >   public void tearUp(){
> >> > > >     User u = new User("testUser","first","last");
> >> > > >     userRepository.save(u);
> >> > > >     assertNotNull(u.getId());
> >> > > >     assertEquals(userRepository.count(), 1);
> >> > > >   }
> >> > > >
> >> > > >   @After
> >> > > >   public void tearDown(){
> >> > > >
> >> > > >
> >> > > >
> >> > >
> >> >
> >>
> userRepository.getEntityManager().remove(userRepository.findUser("testUser"));
> >> > > >   }
> >> > > >
> >> > > >
> >> > > >   @Test
> >> > > >   public void shouldFindUser() {
> >> > > >     User user = userRepository.findUser("testUser");
> >> > > >     assertNotNull(user);
> >> > > >     assertEquals(user.getUserName(),"testUser");
> >> > > >   }
> >> > > >
> >> > > >
> >> > > > }
> >> > > >
> >> > > > I have added deltaspike-jpa module to the project to have
> >> transaction
> >> > > > support:
> >> > > >
> >> > > > @Transactional
> >> > > > public User save(User user) {
> >> > > >     if (user.isTransient()) {
> >> > > >         entityManager.persist(user);
> >> > > >     } else {
> >> > > >         user = entityManager.merge(user);
> >> > > >     }
> >> > > >     return user;
> >> > > > }
> >> > > >
> >> > > > without it, i could not insert users during tests (@Before)
> >> > > >
> >> > > > here is test persistence.xml:
> >> > > >
> >> > > > <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>
> >> > > >       <class>org.os890.demo.ee6.ds.domain.user.User</class>
> >> > > >       <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>
> >> > > >
> >> > > >
> >> > > > finally the dependencies I've added to project:
> >> > > >
> >> > > >       <dependency>
> >> > > >             <groupId>org.apache.deltaspike.modules</groupId>
> >> > > >             <artifactId>deltaspike-jpa-module-api</artifactId>
> >> > > >             <version>${ds.version}</version>
> >> > > >         </dependency>
> >> > > >
> >> > > >         <dependency>
> >> > > >             <groupId>org.apache.deltaspike.modules</groupId>
> >> > > >             <artifactId>deltaspike-jpa-module-impl</artifactId>
> >> > > >             <version>${ds.version}</version>
> >> > > >             <scope>runtime</scope>
> >> > > >         </dependency>
> >> > > >         <dependency>
> >> > > >             <groupId>org.hibernate</groupId>
> >> > > >             <artifactId>hibernate-entitymanager</artifactId>
> >> > > >             <version>4.2.18.Final</version>
> >> > > >             <scope>test</scope>
> >> > > >         </dependency>
> >> > > >
> >> > > >         <dependency>
> >> > > >             <groupId>hsqldb</groupId>
> >> > > >             <artifactId>hsqldb</artifactId>
> >> > > >             <version>1.8.0.10</version>
> >> > > >             <scope>test</scope>
> >> > > >         </dependency>
> >> > > >
> >> > > >
> >> > > > Any other advices?
> >> > > >
> >> > > > I hope it helps someone ;)
> >> > > >
> >> > > > 2015-04-06 17:12 GMT-03:00 Rafael Pestano <rm...@gmail.com>:
> >> > > >
> >> > > > > 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>
> >> > > > >
> >> > > >
> >> > > >
> >> > > >
> >> > > > --
> >> > > > <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>
> >> > > >
> >> > >
> >> >
> >>
> >
>

Re: Best approach for testing repositories

Posted by Rafael Pestano <rm...@gmail.com>.
Hi Karl, didnt saw your message.

I think arquillian configuration is not that hard. I can try to add it to
the github repo mentioned in this thread.
Em 07/04/2015 08:12, "Rafael Pestano" <rm...@gmail.com> escreveu:

> I see,
>
> In simple words I think test control would give more speed and arquillian
> more reliabie tests.
>
> So if we got transaction support and data store management in test control
> I think the module would receive more adoption.
> Em 07/04/2015 06:43, "Thomas Hug" <th...@gmail.com> escreveu:
>
>> Thnx for the clarification Rafael! Most of DS (including the data module)
>> is tested with Arquillian, so can't comment much on a DS test control
>> approach...
>>
>> On Tue, Apr 7, 2015 at 11:30 AM, Rafael Pestano <rm...@gmail.com>
>> wrote:
>>
>> > Hi Thomas, no specific reason, just want to see how far I can go with ds
>> > and know when I need to use a framework or another.
>> > Em 07/04/2015 04:24, "Thomas Hug" <th...@gmail.com> escreveu:
>> >
>> > > Hi Rafael
>> > > Any specific reason not to use Arquillian? You can also integrate the
>> > > persistence extension which works already nicely with DBUnit.
>> > >
>> > > On Tue, Apr 7, 2015 at 12:04 AM, Rafael Pestano <rm...@gmail.com>
>> > > wrote:
>> > >
>> > > > Hi again,
>> > > >
>> > > > I have evolved a bit my example and could managed to test
>> > UserRepository
>> > > > from Gerhard ee6-ds-dm, sources are here:
>> > > > https://github.com/rmpestano/ee6-ds-demo/
>> > > > <https://github.com/rmpestano/ee6-ds-demo/>
>> > > >
>> > > > first thing i did was to disable application EntityManagerProduces
>> in
>> > > unit
>> > > > tests (remember i want to use my in memory db for tests):
>> > > >
>> > > > @Exclude(ifProjectStage = ProjectStage.UnitTest.class)
>> > > > public class EntityManagerProducer {
>> > > >
>> > > >     @PersistenceContext(unitName = "demoApplicationPU")
>> > > >     private EntityManager entityManagerProxy;
>> > > >
>> > > >     @Produces
>> > > >     public EntityManager exposeDependentEntityManager() {
>> > > >         return entityManagerProxy;
>> > > >     }
>> > > >
>> > > > }
>> > > >
>> > > >
>> > > > then i've create my producer in test sources:
>> > > >
>> > > > public class TestEntityManagerProducer  {
>> > > >
>> > > >   private EntityManager entityManager;
>> > > >
>> > > >   @Produces
>> > > >   @ApplicationScoped
>> > > >   public EntityManager exposeDependentEntityManager()
>> > > >   {
>> > > >     entityManager =
>> > > >
>> > Persistence.createEntityManagerFactory("TEST_PU").createEntityManager();
>> > > >     return entityManager;
>> > > >   }
>> > > >
>> > > >   public void closeEntityManager(@Disposes EntityManager
>> entityManager)
>> > > >   {
>> > > >     if (entityManager.isOpen())
>> > > >     {
>> > > >       entityManager.close();
>> > > >     }
>> > > >   }
>> > > >
>> > > > }
>> > > >
>> > > >
>> > > > And here is my repository test:
>> > > >
>> > > > @RunWith(CdiTestRunner.class)
>> > > > @TestControl(projectStage = ProjectStage.UnitTest.class)
>> > > > public class UserRepositoryTest {
>> > > >
>> > > >
>> > > >   @Inject
>> > > >   UserRepository userRepository;
>> > > >
>> > > >
>> > > >   @Before
>> > > >   public void tearUp(){
>> > > >     User u = new User("testUser","first","last");
>> > > >     userRepository.save(u);
>> > > >     assertNotNull(u.getId());
>> > > >     assertEquals(userRepository.count(), 1);
>> > > >   }
>> > > >
>> > > >   @After
>> > > >   public void tearDown(){
>> > > >
>> > > >
>> > > >
>> > >
>> >
>> userRepository.getEntityManager().remove(userRepository.findUser("testUser"));
>> > > >   }
>> > > >
>> > > >
>> > > >   @Test
>> > > >   public void shouldFindUser() {
>> > > >     User user = userRepository.findUser("testUser");
>> > > >     assertNotNull(user);
>> > > >     assertEquals(user.getUserName(),"testUser");
>> > > >   }
>> > > >
>> > > >
>> > > > }
>> > > >
>> > > > I have added deltaspike-jpa module to the project to have
>> transaction
>> > > > support:
>> > > >
>> > > > @Transactional
>> > > > public User save(User user) {
>> > > >     if (user.isTransient()) {
>> > > >         entityManager.persist(user);
>> > > >     } else {
>> > > >         user = entityManager.merge(user);
>> > > >     }
>> > > >     return user;
>> > > > }
>> > > >
>> > > > without it, i could not insert users during tests (@Before)
>> > > >
>> > > > here is test persistence.xml:
>> > > >
>> > > > <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>
>> > > >       <class>org.os890.demo.ee6.ds.domain.user.User</class>
>> > > >       <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>
>> > > >
>> > > >
>> > > > finally the dependencies I've added to project:
>> > > >
>> > > >       <dependency>
>> > > >             <groupId>org.apache.deltaspike.modules</groupId>
>> > > >             <artifactId>deltaspike-jpa-module-api</artifactId>
>> > > >             <version>${ds.version}</version>
>> > > >         </dependency>
>> > > >
>> > > >         <dependency>
>> > > >             <groupId>org.apache.deltaspike.modules</groupId>
>> > > >             <artifactId>deltaspike-jpa-module-impl</artifactId>
>> > > >             <version>${ds.version}</version>
>> > > >             <scope>runtime</scope>
>> > > >         </dependency>
>> > > >         <dependency>
>> > > >             <groupId>org.hibernate</groupId>
>> > > >             <artifactId>hibernate-entitymanager</artifactId>
>> > > >             <version>4.2.18.Final</version>
>> > > >             <scope>test</scope>
>> > > >         </dependency>
>> > > >
>> > > >         <dependency>
>> > > >             <groupId>hsqldb</groupId>
>> > > >             <artifactId>hsqldb</artifactId>
>> > > >             <version>1.8.0.10</version>
>> > > >             <scope>test</scope>
>> > > >         </dependency>
>> > > >
>> > > >
>> > > > Any other advices?
>> > > >
>> > > > I hope it helps someone ;)
>> > > >
>> > > > 2015-04-06 17:12 GMT-03:00 Rafael Pestano <rm...@gmail.com>:
>> > > >
>> > > > > 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>
>> > > > >
>> > > >
>> > > >
>> > > >
>> > > > --
>> > > > <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>
>> > > >
>> > >
>> >
>>
>

Re: Best approach for testing repositories

Posted by Rafael Pestano <rm...@gmail.com>.
I see,

In simple words I think test control would give more speed and arquillian
more reliabie tests.

So if we got transaction support and data store management in test control
I think the module would receive more adoption.
Em 07/04/2015 06:43, "Thomas Hug" <th...@gmail.com> escreveu:

> Thnx for the clarification Rafael! Most of DS (including the data module)
> is tested with Arquillian, so can't comment much on a DS test control
> approach...
>
> On Tue, Apr 7, 2015 at 11:30 AM, Rafael Pestano <rm...@gmail.com>
> wrote:
>
> > Hi Thomas, no specific reason, just want to see how far I can go with ds
> > and know when I need to use a framework or another.
> > Em 07/04/2015 04:24, "Thomas Hug" <th...@gmail.com> escreveu:
> >
> > > Hi Rafael
> > > Any specific reason not to use Arquillian? You can also integrate the
> > > persistence extension which works already nicely with DBUnit.
> > >
> > > On Tue, Apr 7, 2015 at 12:04 AM, Rafael Pestano <rm...@gmail.com>
> > > wrote:
> > >
> > > > Hi again,
> > > >
> > > > I have evolved a bit my example and could managed to test
> > UserRepository
> > > > from Gerhard ee6-ds-dm, sources are here:
> > > > https://github.com/rmpestano/ee6-ds-demo/
> > > > <https://github.com/rmpestano/ee6-ds-demo/>
> > > >
> > > > first thing i did was to disable application EntityManagerProduces in
> > > unit
> > > > tests (remember i want to use my in memory db for tests):
> > > >
> > > > @Exclude(ifProjectStage = ProjectStage.UnitTest.class)
> > > > public class EntityManagerProducer {
> > > >
> > > >     @PersistenceContext(unitName = "demoApplicationPU")
> > > >     private EntityManager entityManagerProxy;
> > > >
> > > >     @Produces
> > > >     public EntityManager exposeDependentEntityManager() {
> > > >         return entityManagerProxy;
> > > >     }
> > > >
> > > > }
> > > >
> > > >
> > > > then i've create my producer in test sources:
> > > >
> > > > public class TestEntityManagerProducer  {
> > > >
> > > >   private EntityManager entityManager;
> > > >
> > > >   @Produces
> > > >   @ApplicationScoped
> > > >   public EntityManager exposeDependentEntityManager()
> > > >   {
> > > >     entityManager =
> > > >
> > Persistence.createEntityManagerFactory("TEST_PU").createEntityManager();
> > > >     return entityManager;
> > > >   }
> > > >
> > > >   public void closeEntityManager(@Disposes EntityManager
> entityManager)
> > > >   {
> > > >     if (entityManager.isOpen())
> > > >     {
> > > >       entityManager.close();
> > > >     }
> > > >   }
> > > >
> > > > }
> > > >
> > > >
> > > > And here is my repository test:
> > > >
> > > > @RunWith(CdiTestRunner.class)
> > > > @TestControl(projectStage = ProjectStage.UnitTest.class)
> > > > public class UserRepositoryTest {
> > > >
> > > >
> > > >   @Inject
> > > >   UserRepository userRepository;
> > > >
> > > >
> > > >   @Before
> > > >   public void tearUp(){
> > > >     User u = new User("testUser","first","last");
> > > >     userRepository.save(u);
> > > >     assertNotNull(u.getId());
> > > >     assertEquals(userRepository.count(), 1);
> > > >   }
> > > >
> > > >   @After
> > > >   public void tearDown(){
> > > >
> > > >
> > > >
> > >
> >
> userRepository.getEntityManager().remove(userRepository.findUser("testUser"));
> > > >   }
> > > >
> > > >
> > > >   @Test
> > > >   public void shouldFindUser() {
> > > >     User user = userRepository.findUser("testUser");
> > > >     assertNotNull(user);
> > > >     assertEquals(user.getUserName(),"testUser");
> > > >   }
> > > >
> > > >
> > > > }
> > > >
> > > > I have added deltaspike-jpa module to the project to have transaction
> > > > support:
> > > >
> > > > @Transactional
> > > > public User save(User user) {
> > > >     if (user.isTransient()) {
> > > >         entityManager.persist(user);
> > > >     } else {
> > > >         user = entityManager.merge(user);
> > > >     }
> > > >     return user;
> > > > }
> > > >
> > > > without it, i could not insert users during tests (@Before)
> > > >
> > > > here is test persistence.xml:
> > > >
> > > > <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>
> > > >       <class>org.os890.demo.ee6.ds.domain.user.User</class>
> > > >       <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>
> > > >
> > > >
> > > > finally the dependencies I've added to project:
> > > >
> > > >       <dependency>
> > > >             <groupId>org.apache.deltaspike.modules</groupId>
> > > >             <artifactId>deltaspike-jpa-module-api</artifactId>
> > > >             <version>${ds.version}</version>
> > > >         </dependency>
> > > >
> > > >         <dependency>
> > > >             <groupId>org.apache.deltaspike.modules</groupId>
> > > >             <artifactId>deltaspike-jpa-module-impl</artifactId>
> > > >             <version>${ds.version}</version>
> > > >             <scope>runtime</scope>
> > > >         </dependency>
> > > >         <dependency>
> > > >             <groupId>org.hibernate</groupId>
> > > >             <artifactId>hibernate-entitymanager</artifactId>
> > > >             <version>4.2.18.Final</version>
> > > >             <scope>test</scope>
> > > >         </dependency>
> > > >
> > > >         <dependency>
> > > >             <groupId>hsqldb</groupId>
> > > >             <artifactId>hsqldb</artifactId>
> > > >             <version>1.8.0.10</version>
> > > >             <scope>test</scope>
> > > >         </dependency>
> > > >
> > > >
> > > > Any other advices?
> > > >
> > > > I hope it helps someone ;)
> > > >
> > > > 2015-04-06 17:12 GMT-03:00 Rafael Pestano <rm...@gmail.com>:
> > > >
> > > > > 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>
> > > > >
> > > >
> > > >
> > > >
> > > > --
> > > > <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>
> > > >
> > >
> >
>

Re: Best approach for testing repositories

Posted by Thomas Hug <th...@gmail.com>.
Thnx for the clarification Rafael! Most of DS (including the data module)
is tested with Arquillian, so can't comment much on a DS test control
approach...

On Tue, Apr 7, 2015 at 11:30 AM, Rafael Pestano <rm...@gmail.com> wrote:

> Hi Thomas, no specific reason, just want to see how far I can go with ds
> and know when I need to use a framework or another.
> Em 07/04/2015 04:24, "Thomas Hug" <th...@gmail.com> escreveu:
>
> > Hi Rafael
> > Any specific reason not to use Arquillian? You can also integrate the
> > persistence extension which works already nicely with DBUnit.
> >
> > On Tue, Apr 7, 2015 at 12:04 AM, Rafael Pestano <rm...@gmail.com>
> > wrote:
> >
> > > Hi again,
> > >
> > > I have evolved a bit my example and could managed to test
> UserRepository
> > > from Gerhard ee6-ds-dm, sources are here:
> > > https://github.com/rmpestano/ee6-ds-demo/
> > > <https://github.com/rmpestano/ee6-ds-demo/>
> > >
> > > first thing i did was to disable application EntityManagerProduces in
> > unit
> > > tests (remember i want to use my in memory db for tests):
> > >
> > > @Exclude(ifProjectStage = ProjectStage.UnitTest.class)
> > > public class EntityManagerProducer {
> > >
> > >     @PersistenceContext(unitName = "demoApplicationPU")
> > >     private EntityManager entityManagerProxy;
> > >
> > >     @Produces
> > >     public EntityManager exposeDependentEntityManager() {
> > >         return entityManagerProxy;
> > >     }
> > >
> > > }
> > >
> > >
> > > then i've create my producer in test sources:
> > >
> > > public class TestEntityManagerProducer  {
> > >
> > >   private EntityManager entityManager;
> > >
> > >   @Produces
> > >   @ApplicationScoped
> > >   public EntityManager exposeDependentEntityManager()
> > >   {
> > >     entityManager =
> > >
> Persistence.createEntityManagerFactory("TEST_PU").createEntityManager();
> > >     return entityManager;
> > >   }
> > >
> > >   public void closeEntityManager(@Disposes EntityManager entityManager)
> > >   {
> > >     if (entityManager.isOpen())
> > >     {
> > >       entityManager.close();
> > >     }
> > >   }
> > >
> > > }
> > >
> > >
> > > And here is my repository test:
> > >
> > > @RunWith(CdiTestRunner.class)
> > > @TestControl(projectStage = ProjectStage.UnitTest.class)
> > > public class UserRepositoryTest {
> > >
> > >
> > >   @Inject
> > >   UserRepository userRepository;
> > >
> > >
> > >   @Before
> > >   public void tearUp(){
> > >     User u = new User("testUser","first","last");
> > >     userRepository.save(u);
> > >     assertNotNull(u.getId());
> > >     assertEquals(userRepository.count(), 1);
> > >   }
> > >
> > >   @After
> > >   public void tearDown(){
> > >
> > >
> > >
> >
> userRepository.getEntityManager().remove(userRepository.findUser("testUser"));
> > >   }
> > >
> > >
> > >   @Test
> > >   public void shouldFindUser() {
> > >     User user = userRepository.findUser("testUser");
> > >     assertNotNull(user);
> > >     assertEquals(user.getUserName(),"testUser");
> > >   }
> > >
> > >
> > > }
> > >
> > > I have added deltaspike-jpa module to the project to have transaction
> > > support:
> > >
> > > @Transactional
> > > public User save(User user) {
> > >     if (user.isTransient()) {
> > >         entityManager.persist(user);
> > >     } else {
> > >         user = entityManager.merge(user);
> > >     }
> > >     return user;
> > > }
> > >
> > > without it, i could not insert users during tests (@Before)
> > >
> > > here is test persistence.xml:
> > >
> > > <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>
> > >       <class>org.os890.demo.ee6.ds.domain.user.User</class>
> > >       <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>
> > >
> > >
> > > finally the dependencies I've added to project:
> > >
> > >       <dependency>
> > >             <groupId>org.apache.deltaspike.modules</groupId>
> > >             <artifactId>deltaspike-jpa-module-api</artifactId>
> > >             <version>${ds.version}</version>
> > >         </dependency>
> > >
> > >         <dependency>
> > >             <groupId>org.apache.deltaspike.modules</groupId>
> > >             <artifactId>deltaspike-jpa-module-impl</artifactId>
> > >             <version>${ds.version}</version>
> > >             <scope>runtime</scope>
> > >         </dependency>
> > >         <dependency>
> > >             <groupId>org.hibernate</groupId>
> > >             <artifactId>hibernate-entitymanager</artifactId>
> > >             <version>4.2.18.Final</version>
> > >             <scope>test</scope>
> > >         </dependency>
> > >
> > >         <dependency>
> > >             <groupId>hsqldb</groupId>
> > >             <artifactId>hsqldb</artifactId>
> > >             <version>1.8.0.10</version>
> > >             <scope>test</scope>
> > >         </dependency>
> > >
> > >
> > > Any other advices?
> > >
> > > I hope it helps someone ;)
> > >
> > > 2015-04-06 17:12 GMT-03:00 Rafael Pestano <rm...@gmail.com>:
> > >
> > > > 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>
> > > >
> > >
> > >
> > >
> > > --
> > > <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>
> > >
> >
>

Re: Best approach for testing repositories

Posted by Thomas Hug <th...@gmail.com>.
Hi Rafael
Any specific reason not to use Arquillian? You can also integrate the
persistence extension which works already nicely with DBUnit.

On Tue, Apr 7, 2015 at 12:04 AM, Rafael Pestano <rm...@gmail.com> wrote:

> Hi again,
>
> I have evolved a bit my example and could managed to test UserRepository
> from Gerhard ee6-ds-dm, sources are here:
> https://github.com/rmpestano/ee6-ds-demo/
> <https://github.com/rmpestano/ee6-ds-demo/>
>
> first thing i did was to disable application EntityManagerProduces in unit
> tests (remember i want to use my in memory db for tests):
>
> @Exclude(ifProjectStage = ProjectStage.UnitTest.class)
> public class EntityManagerProducer {
>
>     @PersistenceContext(unitName = "demoApplicationPU")
>     private EntityManager entityManagerProxy;
>
>     @Produces
>     public EntityManager exposeDependentEntityManager() {
>         return entityManagerProxy;
>     }
>
> }
>
>
> then i've create my producer in test sources:
>
> public class TestEntityManagerProducer  {
>
>   private EntityManager entityManager;
>
>   @Produces
>   @ApplicationScoped
>   public EntityManager exposeDependentEntityManager()
>   {
>     entityManager =
> Persistence.createEntityManagerFactory("TEST_PU").createEntityManager();
>     return entityManager;
>   }
>
>   public void closeEntityManager(@Disposes EntityManager entityManager)
>   {
>     if (entityManager.isOpen())
>     {
>       entityManager.close();
>     }
>   }
>
> }
>
>
> And here is my repository test:
>
> @RunWith(CdiTestRunner.class)
> @TestControl(projectStage = ProjectStage.UnitTest.class)
> public class UserRepositoryTest {
>
>
>   @Inject
>   UserRepository userRepository;
>
>
>   @Before
>   public void tearUp(){
>     User u = new User("testUser","first","last");
>     userRepository.save(u);
>     assertNotNull(u.getId());
>     assertEquals(userRepository.count(), 1);
>   }
>
>   @After
>   public void tearDown(){
>
>
> userRepository.getEntityManager().remove(userRepository.findUser("testUser"));
>   }
>
>
>   @Test
>   public void shouldFindUser() {
>     User user = userRepository.findUser("testUser");
>     assertNotNull(user);
>     assertEquals(user.getUserName(),"testUser");
>   }
>
>
> }
>
> I have added deltaspike-jpa module to the project to have transaction
> support:
>
> @Transactional
> public User save(User user) {
>     if (user.isTransient()) {
>         entityManager.persist(user);
>     } else {
>         user = entityManager.merge(user);
>     }
>     return user;
> }
>
> without it, i could not insert users during tests (@Before)
>
> here is test persistence.xml:
>
> <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>
>       <class>org.os890.demo.ee6.ds.domain.user.User</class>
>       <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>
>
>
> finally the dependencies I've added to project:
>
>       <dependency>
>             <groupId>org.apache.deltaspike.modules</groupId>
>             <artifactId>deltaspike-jpa-module-api</artifactId>
>             <version>${ds.version}</version>
>         </dependency>
>
>         <dependency>
>             <groupId>org.apache.deltaspike.modules</groupId>
>             <artifactId>deltaspike-jpa-module-impl</artifactId>
>             <version>${ds.version}</version>
>             <scope>runtime</scope>
>         </dependency>
>         <dependency>
>             <groupId>org.hibernate</groupId>
>             <artifactId>hibernate-entitymanager</artifactId>
>             <version>4.2.18.Final</version>
>             <scope>test</scope>
>         </dependency>
>
>         <dependency>
>             <groupId>hsqldb</groupId>
>             <artifactId>hsqldb</artifactId>
>             <version>1.8.0.10</version>
>             <scope>test</scope>
>         </dependency>
>
>
> Any other advices?
>
> I hope it helps someone ;)
>
> 2015-04-06 17:12 GMT-03:00 Rafael Pestano <rm...@gmail.com>:
>
> > 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>
> >
>
>
>
> --
> <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>
>