1 2 Previous Next 22 Replies Latest reply on Jan 20, 2012 1:34 PM by diegosantiviago Go to original post
      • 15. Re: TestNG & Maven: How to configure it correctly
        dan.j.allen

        To illustrate the problem better here is the structure of the project:


        MyProjEJB
        ->bootstrap
        ->lib (libs of the generated SeamGen-project)
        ->src
                   ->main
                         ->java(Beans and interfaces inside) 
                         ->resources
                                     ->META-INF
                                               ->ejb-jar.xml
                                               ->persistence.xml(for the application -> jdbc/myproj is defined there)
                   ->test
                   ->java
                         ->MyTest.java
                   ->resources
                             ->META-INF
                                               ->persistence.properties
                                               ->persistence-test.xml(for the test -> java:/DefaultDS is defined there)
        ->target
                   ->classes
                   ->surefire-reports
                   ->test-classes
        ->pom.xml





        So your problem here is that you have a file named persistence-test.xml in src/test/resources/META-INF. That isn't going to work. The file needs to be named persistence.xml, the same as it is named in the src/main/resources/META-INF. So rename that back to persistence.xml. Also, add a components.xml to src/main/resources/META-INF and make sure that <core:init> has the jndiPattern for JBoss Embedded that I mentioned above. What you want is for your test classes/resources to take precedence over your non-test classes/resources.


        I'll give this another test myself just to see if I find out any other important tips.

        • 16. Re: TestNG & Maven: How to configure it correctly
          dan.j.allen

          Ah, I see the problem. Since there are two classpaths, thus two persistence.xml files, Embedded JBoss attempts to load the persistence unit defined in each of them (src/main/resources/META-INF/persistence.xml and src/test/resources/META-INF/persistence.xml). I didn't run into this problem because in my tests I was using the persistence unit defined in the main classpath (I didn't have an override for tests).


          The absolutely simplest solution is to use a separate project for tests that depends on your original project. I'm guessing then the persistence.xml from the original JAR won't be picked up (you have to explicitly reference a persistence unit in a JAR to get it to load).


          But you can also just use the antrun plugin to move files to the side during the test execution. It's a little ugly, but really it adds great flexibility and you might find it useful to have anyway.


          Here I am just moving the main persistence.xml out of the way during test execution. Note that antrun executes after each phase, something that is not documented in the plugin reference at all (but is a very important thing to know).


          <plugin>
              <artifactId>maven-antrun-plugin</artifactId>
              <executions>
                  <execution>
                      <id>pre-test</id>
                      <phase>process-test-resources</phase>
                      <goals>
                          <goal>run</goal>
                      </goals>
                      <configuration>
                          <tasks>
                             <move file="${project.build.outputDirectory}/META-INF/persistence.xml"
                                tofile="${project.build.outputDirectory}/META-INF/persistence.xml~"/>
                          </tasks>
                      </configuration>
                  </execution>
                  <execution>
                      <id>post-test</id>
                      <phase>test</phase>
                      <goals>
                          <goal>run</goal>
                      </goals>
                      <configuration>
                          <tasks>
                             <move file="${project.build.outputDirectory}/META-INF/persistence.xml~"
                                tofile="${project.build.outputDirectory}/META-INF/persistence.xml"/>
                          </tasks>
                      </configuration>
                  </execution>
              </executions>
          </plugin>

          • 17. Re: TestNG & Maven: How to configure it correctly
            dan.j.allen

            Here's an even cleaner way. It takes a list of files (or file patterns) and adds a ~ to the end of the file name, effectively hiding it during test execution. You could use a backup folder instead if you wanted.


            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>pre-test</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <move todir="${project.build.outputDirectory}">
                                    <fileset dir="${project.build.outputDirectory}">
                                        <!-- include list of files you want to have hidden during tests -->
                                        <include name="META-INF/persistence.xml"/>
                                    </fileset>
                                    <mapper type="glob" from="*" to="*~"/>
                                </move>
                            </tasks>
                        </configuration>
                    </execution>
                    <execution>
                        <id>post-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <move todir="${project.build.outputDirectory}">
                                    <fileset dir="${project.build.outputDirectory}" defaultexcludes="false">
                                        <include name="**/*~"/>
                                    </fileset>
                                    <mapper type="glob" from="*~" to="*"/>
                                </move>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            • 18. Re: TestNG & Maven: How to configure it correctly

              That's awesome. Thanks Dan.

              • 19. Re: TestNG & Maven: How to configure it correctly
                troy.sellers

                Thanks heaps Dan...
                A much neater solution which is probably going to be needed for a while, this issue doesn't have priority with the maven team.


                Cheers,

                • 20. Re: TestNG & Maven: How to configure it correctly
                  barakka

                  Interesting approach.


                  However, from my experience with using both maven and the Test-NG plugin within eclipse, this might not be enough to have both systems working. The problem is that the maven plugin for eclipse does not completely honors the maven classpaths and the test-ng plugin will not execute your renaming task.


                  In the end, my solution was to mess a little with the SeamTest class and redefine the way the embedded-jboss is bootstraped. There, a part from having a little more control over the whole process, I'm using a custom classpath that filters out everything that is not needed / wanted. This is most important when your maven solution is composed of several projects, and you want to use the Resolve workspace artifacts in the eclipse maven plugin.


                  My two cents,
                  Riccardo.

                  • 21. Re: TestNG & Maven: How to configure it correctly
                    mcarrizo

                    After searching a lot, I find this very ugly solution:





                         <plugin>
                              <groupId>org.apache.maven.plugins</groupId>
                              <artifactId>maven-antrun-plugin</artifactId>
                                    <executions>
                                        <execution>
                                            <id>setupTestClasspath</id>
                                            <phase>test-compile</phase>
                                            <configuration>
                                                <tasks>
                                                    <echo message="Setting up a single merged test classpath directory" />
                                                    <!-- move the originals -->
                                                    <mkdir dir="${basedir}/target/tmp" />
                                                    <move file="${basedir}/target/classes" todir="${basedir}/target/tmp" />
                                                    <move file="${basedir}/target/test-classes" todir="${basedir}/target/tmp" />
                                                    <!-- Merge into new directory -->
                                                    <copy todir="${basedir}/target/test-classes" overwrite="true">
                                                        <fileset dir="${basedir}/target/tmp/test-classes" />
                                                    </copy>
                                                    <copy todir="${basedir}/target/test-classes" overwrite="false">
                                                        <fileset dir="${basedir}/target/tmp/classes" />
                                                    </copy>
                                                    <mkdir dir="${basedir}/target/classes" />
                                                </tasks>
                                            </configuration>
                                            <goals>
                                                <goal>run</goal>
                                            </goals>
                                        </execution>
                                        <execution>
                                            <id>restoreTestClasspath</id>
                                            <phase>test</phase>
                                            <configuration>
                                                <tasks>
                                                    <echo message="Restoring original classes directories" />
                                                    <delete dir="${basedir}/target/classes" />
                                                    <move file="${basedir}/target/test-classes" tofile="${basedir}/target/test-classes-MERGED" />
                                                    <move file="${basedir}/target/tmp/classes" todir="${basedir}/target" />
                                                    <move file="${basedir}/target/tmp/test-classes" todir="${basedir}/target" />
                                                </tasks>
                                            </configuration>
                                            <goals>
                                                <goal>run</goal>
                                            </goals>
                                        </execution>
                                    </executions>
                                </plugin>
                    



                    • 22. Re: TestNG & Maven: How to configure it correctly
                      diegosantiviago
                      Hello,

                      Using:

                      mvn archetype:generate -DarchetypeCatalog=http://open-archetypes.github.com/maven-repo/snapshots/archetype-catalog.xml

                      With seam-ear-archetype, generates:

                      1. /project-ear/
                      2. /project-ejb/
                      3. /project-tests/
                      4. /project-util/
                      5. /project-web/
                      6. /pom.xml

                      Changing /project-tests/src/test/resources/META-INF/project-ds.xml:

                      <connection-url>jdbc:mysql://localhost:3306/projecttest/connection-url>

                      Changing /project-ejb/src/main/resources/META-INF/persistence.xml, because its a "production configuration".

                      <property name="hibernate.hbm2ddl.auto" value="validate"/>

                      And executing "mvn install" in /pom.xml, generates HibernateException:

                      Caused by: org.hibernate.HibernateException: Missing table: User

                      [INFO] project application .............................. SUCCESS [0.344s]
                      [INFO] project utility module ........................... SUCCESS [3.080s]
                      [INFO] project EJB module ............................... SUCCESS [1.659s]
                      [INFO] project Web module ............................... SUCCESS [1.212s]
                      [INFO] project EAR module ............................... SUCCESS [1.250s]
                      [INFO] project integration tests module ................. FAILURE [9.231s]

                      How I can override persistence.xml properties (to create-drop in tests phase)? I tried change project-tests/src/test/bootstrap/META-INF/persistence.properties, without success.

                      1 2 Previous Next