Executing Maven unit tests with classpath set to generated project JAR

心不动则不痛 提交于 2019-12-05 02:09:30

问题


Some unit tests in my application are related to finding and manipulating certain files resources that are part of the application itself. I need these tests to be executed in the real production setting of the application, where it is deployed as a JAR file, not as an exploded directory.

How could I instruct Maven to execute my unit tests considering as the classpath the project generated jar file (and any other declared library dependencies) instead of the compiled classes in the file system as it does by default?.

In other words, right now the classpath for my unit tests is set to: /$PROJECTPATH/target/classes/.

Instead, I would like this classpath to be set to: /$PROJECTPATH/target/myjarfile.jar.

I have tried manually adding and removing dependency classes, as explained here: http://maven.apache.org/plugins/maven-surefire-plugin/examples/configuring-classpath.html ,but until now it is not working.

My current project POM looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.mygroupid</groupId>
  <artifactId>myartifact</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <dependencies>
      ...
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.1</version>
        <configuration>
        <source>1.7</source>
        <target>1.7</target>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <version>2.1.2</version> 
        <executions>
          <execution>
            <id>attach-sources</id>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.4</version>

        <configuration>
              <includeScope>runtime</includeScope>
              <outputDirectory>${project.build.directory}/lib</outputDirectory>
        </configuration>

        <executions>
          <execution> 
            <id>copy-dependencies</id>
            <phase>process-resources</phase>
            <!-- <phase>package</phase>  -->
            <goals>
              <goal>copy-dependencies</goal>
            </goals>

          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.3</version>
        <configuration>
              <classpathDependencyExcludes>
                <classpathDependencyExclude>
                  ${project.build.outputDirectory}
                </classpathDependencyExclude>
              </classpathDependencyExcludes>

              <additionalClasspathElements>
                <additionalClasspathElement>
              ${project.build.directory}/${project.build.finalName}.${project.packaging}
                </additionalClasspathElement>
              </additionalClasspathElements>
        </configuration>
      </plugin>

    </plugins>

  </build>
</project>

Thanks in advance for any help!.


回答1:


The standard unit tests executed as part of the test lifecycle phase cannot see the project JAR because the test phase is executed before the package phase, so your tests are run before Maven generates the JAR. See this page for a list of lifecycle phases and their order.

What you want it to run your tests as integration tests, which execute in the integration-test phase.

There are a number of tutorials for setting up Maven to run integration tests. Here and here are a couple of starters. The failsafe plugin is typically used for executing integration tests.

I can't recall exactly if integration tests use target/classes or your project's JAR file in the classpath. But if it doesn't you could always create another Maven project, add your tests in there and add the main project as a dependency to this integration test project. In some cases this can be preferable to using the integration test phase in the main project if it is not just a standard Java library, for example if you are writing an annotation processor.



来源:https://stackoverflow.com/questions/12198047/executing-maven-unit-tests-with-classpath-set-to-generated-project-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!