Jomikel
Jomikel

Reputation: 41

Testing a Java EE Application with Maven

i'm really new to the world of Java EE! I have build an Enterprise Application that works fine. Because i doesn't want IDE specific features and no embedded Application Server i'm deploying the Application always with

<groupId>org.glassfish.maven.plugin</groupId>
<artifactId>maven-glassfish-plugin</artifactId>
<version>2.1</version>

to my testing Glassfish 4.1!

All works fine until i wrote my first Unit test. I had a lot of trouble because i need for the Unit Test an EJB Container. Therefore i have added

<groupId>org.glassfish.embedded</groupId>
<artifactId>maven-embedded-glassfish-plugin</artifactId>
<version>4.0</version>

to my EJB module. After that i have added a glassfish-resources.xml to the EJB module:

<resources>
    <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0"
                          connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false"
                          connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit"
                          datasource-classname="org.postgresql.ds.PGSimpleDataSource" fail-all-connections="false"
                          idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true"
                          lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false"
                          max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="embeddedConnPool"
                          non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource"
                          statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0"
                          validation-table-name="DUAL" wrap-jdbc-objects="false">
        <property name="URL" value="jdbc:postgresql://localhost:5432/mes"/>
        <property name="User" value="mes"/>
        <property name="Password" value="mes"/>
    </jdbc-connection-pool>
    <jdbc-resource jndi-name="jdbc/mes2" pool-name="embeddedConnPool" enabled="true" />
</resources>

(Before i have used an JDBC resource that was setted up in the DAS). In my perstistence.xml i have:

<jta-data-source>java:app/jdbc/mes2</jta-data-source>

With this settings my Unit test is successfully executed, but i cannot deploy the Application to my testing Glassfish 4.1. If i try, then following Exception occurs:

[ERROR] remote failure: Error occurred during deployment: Exception while preparing the app : Invalid resource : { ResourceInfo : (jndiName=java:app/jdbc/mes2__pm), (applicationName=mes-ear) }. Please see server.log for more details.
[ERROR] Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method : java.lang.RuntimeException: Invalid resource : { ResourceInfo : (jndiName=java:app/jdbc/mes2__pm), (applicationName=mes-ear) }
[ERROR] Invalid resource : { ResourceInfo : (jndiName=java:app/jdbc/mes2__pm), (applicationName=mes-ear) }

I'm not sure what to do. Either my Unit test is successfull and deploy doesn't work or my Unit Test throws Exceptions but deploy works. I hope it is a simple mistake in my configuration.

Has anyone suggestions what to do?

If i change my persistence.xml to:

<jta-data-source>jdbc/mes</jta-data-source>

and my glassfish-resource.xml to:

<jdbc-resource jndi-name="jdbc/mes" pool-name="embeddedConnPool" enabled="true" />

then i can deploy my Application but the Unit test crashes: SCHWERWIEGEND: Invalid resource : jdbc/mes__pm

I'm not sure what to do. Either my Unit test is successfull and deploy doesn't work or my Unit Test throws Exceptions but deploy works. I hope it is a simple mistake in my configuration.

Has anyone suggestions what to do? (I hope someone understand my gibber)

regards, jo

Upvotes: 0

Views: 405

Answers (2)

fjavierm
fjavierm

Reputation: 36

If you are implementing unit test is not mandatory to deploy in a EJB container. You can use for the DAO layer and in memory DB to execute and test all your methods one by one, and to test the service layer (EJBs usually) you can use a framework like Mockito both things supported by JUnit without problems. I´ve read your previous comment that you don´t want to add more frameworks but I think that this is going to be difficult to avoid. I hope it helps.

Upvotes: 0

jensp
jensp

Reputation: 121

Maybe the Arquillian Testing framework would help: http://arquillian.org/

Upvotes: 2

Related Questions