deltaspike-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rafael Pestano <rmpest...@gmail.com>
Subject Re: Best approach for testing repositories
Date Tue, 07 Apr 2015 11:17:28 GMT
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" <rmpestano@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" <thomas.hug@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" <thomas.hug@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>
>> > > >
>> > >
>> >
>>
>

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