jeudi 25 août 2016

Using persistence.xml with tests, strange behaviour

Some strange things, I cannot explain, help is needed.

What happens:

I have a test that is run via maven, it uses src/test/resources/META-INF/persistence.xml, embedded derby server, test is run within non-managed environment, transaction-type="RESOURCE_LOCAL".

It works fine.

Then I add exactly the same test method, but with another name. It also works fine. In the maven log file we can see that both tests are passed:

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

Ok, I add a src/main/resources/META-INF/persistence.xml into the main resource folder. This file should not be used by tests when I ran mvn clean install.

Now according to maven log only one test failed, the 2nd one passed! Here is maven log:

Tests run: 2, Failures: 0, Errors: 1, Skipped: 0

The content of src/main/resources/META-INF/persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://ift.tt/1cKbVbQ"
             xmlns:xsi="http://ift.tt/ra1lAU"
             xsi:schemaLocation="http://ift.tt/1cKbVbQ http://ift.tt/1kMb4sd"
             version="2.1">

    <persistence-unit name="usersPersistenceUnit" transaction-type="JTA">
        <jta-data-source>java:/usersPersistenseUnitJndi</jta-data-source>
        <jar-file>jpa_repository.jar</jar-file>
    </persistence-unit>

</persistence>

It says to use jpa_repository.jar as a file where jpa entities can be found, and when I run tests, this file is not available yet. In the log the error looks:

Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: sun.misc.Launcher$AppClassLoader@6bc7c054
Internal Exception: java.lang.RuntimeException: url = [file:/home/alexandr/projects/jpa/jpa_repository_module/target/classes/jpa_repository.jar]
Caused by: java.lang.RuntimeException: url = [file:/home/alexandr/projects/jpa/jpa_repository_module/target/classes/jpa_repository.jar]
Caused by: java.io.FileNotFoundException: /home/alexandr/projects/jpa/jpa_repository_module/target/classes/jpa_repository.jar (No such file or directory)

I cannot explain it. Why one test according to the logs passed, but another one failed? According to the error when persistence.xml exists in main resource folder, is is used by tests. But it should not.

As soon as I renamed persistence.xml to something like QQQpersistence.xml in main resources, both tests worked fine.

The src/test/resources/META-INF/persistence.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://ift.tt/UICAJV" xmlns:xsi="http://ift.tt/ra1lAU" xsi:schemaLocation="http://ift.tt/UICAJV http://ift.tt/O9YdEP">
    <persistence-unit name="usersPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>com.savdev.jpa.entity.UserEntity</class>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:derby:./sample;create=true"/>
            <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
            <property name="eclipselink.logging.level" value="FINE"/>
        </properties>
    </persistence-unit>
</persistence>

pom.xml

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>2.5.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derby</artifactId>
    <version>10.9.1.0</version>
    <scope>test</scope>
</dependency>
<!--test scope dependencies end-->

jpa_repository org.apache.maven.plugins maven-surefire-plugin ${basedir}/target/

RepositoryServiceTestBase, base test class:

EntityManager entityManager; CrudServiceBean crudService;

@Before public void initEntityManagerAndStartTransaction() throws IOException { EntityManagerFactory emf = Persistence.createEntityManagerFactory("usersPersistenceUnit"); this.crudService = new CrudServiceBean(); this.entityManager = emf.createEntityManager(); crudService.em = entityManager; this.entityManager.getTransaction().begin(); } @After public void stopTransaction() throws IOException { this.entityManager.getTransaction().commit(); }

Actual test:

public class UserRepositoryServiceBeanTest extends RepositoryServiceTestBase {
    @Before
    public void instantiate() {
        this.userRepositoryServiceBean = new UserRepositoryServiceBean();
        this.userRepositoryServiceBean.crudService = crudServiceInstance();
    }

    @Test
    public void createUser(){
        UserEntity userEntity = new UserEntity();
        userEntity.setName(USER_NAME);
        Assert.assertEquals(0, userEntity.getId());
        UserEntity createdUserEntity = userRepositoryServiceBean.create(userEntity);
        Assert.assertTrue(createdUserEntity.getId() > 0);
        Assert.assertEquals(userEntity.getName(), createdUserEntity.getName());
    }

    @Test
    public void createUser2(){
        UserEntity userEntity = new UserEntity();
        userEntity.setName(USER_NAME);
        Assert.assertEquals(0, userEntity.getId());
        UserEntity createdUserEntity = userRepositoryServiceBean.create(userEntity);
        Assert.assertTrue(createdUserEntity.getId() > 0);
        Assert.assertEquals(userEntity.getName(), createdUserEntity.getName());
    }

Aucun commentaire:

Enregistrer un commentaire