5 Replies Latest reply: Jun 6, 2013 4:37 AM by 1009303 RSS

    JPA not working correctly in JUnit tests

    1009303
      Hi there,

      I posted a question here regarding some strange behaviour when using JPA (EclipseLink specifically): http://www.eclipse.org/forums/index.php/t/488417/

      Basically I couldn't run NamedQueries when running JUnit tests - sometimes, but in other times it works.

      I then changed my code to use entityManager.createQuery(...) but now I'm getting: The abstract schema type '...' is unknown.

      Also noticed, when everything's running fine EclipseLink puts a lot of stuff in log but when I experience this issue I can only see about 3 lines of log apart from the exception stack.

      This tells me there's something wrong with the JPA connection.

      What is really strange, I can run it fine say about 5 times and then I got the error. Sometimes it doesn't even run for the first time.

      Did anyone experience such issues?
        • 1. Re: JPA not working correctly in JUnit tests
          gimbal2
          Yes well, wait for an answer in the EclipseLink forum. JPA is only an API, it doesn't "do" anything. It is EclipseLink that is giving you troubles.

          I would advise you to add more information to your Eclipse forum post, such as the persistence.xml file and the code which you use to obtain an entity manager.
          • 2. Re: JPA not working correctly in JUnit tests
            r035198x
            Are the unit tests using derby? Do you have schema names set on some entities? Does your url to the database contain a schema name?
            • 3. Re: JPA not working correctly in JUnit tests
              1009303
              I posted here because this might not be an EclipseLink issue.

              After I wrote this post I ran my unit test from command line (I'm on OS X so from Terminal) instead of Eclipse. This is a Maven project so it was easy: mvn clean test

              It ran fine. Then I tried it again and again, altogether 30 times(!) today and it was all fine with no errors when I ran it from the command line.
              My Eclipse IDE, I usually use to run tests from, has obviously the JUnit plugin running, the m2e plugin, as this is a Maven project and all my project dependencies.

              Maybe it couldn't handle all these, but as I said it's looking all fine from Terminal.

              Here are the details of my setup anyway, to answer your question:

              The unit test is using MySQL with InnoDB tables.

              No schema name is set on entities but there is one in the JDBC connection string used in persistence.xml

                   <persistence-unit name="data-warehouse" transaction-type="RESOURCE_LOCAL">
                        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
                             <class>...</class>
                             <class>...</class>
                             ...
                        <exclude-unlisted-classes>true</exclude-unlisted-classes>
                        <properties>
                             <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
                             <property name="javax.persistence.jdbc.url" value="jdbc:mysql://rdbms.local:3306/test_cms?characterEncoding=utf8" />
                             <property name="javax.persistence.jdbc.user" value="..." />
                             <property name="javax.persistence.jdbc.password" value="..." />
                             <property name="eclipselink.ddl-generation.table-creation-suffix" value="engine=InnoDB" />
                             <property name="eclipselink.ddl-generation.output-mode" value="database" />
                             <property name="eclipselink.ddl-generation" value="create-tables" />
                             <property name="eclipselink.weaving" value="false" />
                             <property name="eclipselink.cache.shared.default" value="false"/>
                        </properties>
                   </persistence-unit>


              ... used where I don't wanna disclose info, this is a proprietary project.
              • 4. Re: JPA not working correctly in JUnit tests
                r035198x
                Typically for a resource (database) bound test like yours one would get more inconsistent results when running via maven directly from the command line than from the IDE. This is because the IDE usually runs your tests one at a time while maven may execute your tests in parallel. It is generally good to write your tests in such a way that it doesn't matter in how many are being run at the same time. I'm surprised you are using mysql here not an in memory database which would make these more integration tests that unit but I'm digressing ...

                Check the maven defaults for whether it's forking or running parallel per class/test. That may be able to help you understand why it would only work when run using maven settings only.
                • 5. Re: JPA not working correctly in JUnit tests
                  1009303
                  Hi,

                  you're right with the in-memory database, we'll use Apache Derby when the project is done it's just I'm using MySQL when developing, so I can see my database :)

                  I never thought about this parallel execution, thanks for the info, I'll do a bit of research and will post it here what I've found.