执行Maven单元测试,并将classpath设置为生成的项目JAR [英] Executing Maven unit tests with classpath set to generated project JAR

查看:176
本文介绍了执行Maven单元测试,并将classpath设置为生成的项目JAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中的某些单元测试与查找和操作属于应用程序本身的某些文件资源有关。
我需要在应用程序的实际生产设置中执行这些测试,将其部署为JAR文件,而不是展开目录。

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.

我怎么能指示Maven执行我的单元测试,考虑到项目生成jar文件(以及任何其他声明的库依赖项)的类路径而不是默认情况下文件系统中的编译类?

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?.

换句话说,现在我的单元测试的类路径设置为: / $ PROJECTPATH / target / classes /

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

相反,我希望将此类路径设置为: / $ PROJECTPATH / target / myjarfile.jar

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

我试过手动添加和删除依赖类,如下所述:
http://maven.apache.org/plugins/maven-surefire-plugin/examples/configuring-classpath.html
,但直到现在它还不是orking。

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.

我当前的项目POM如下所示:

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>

提前感谢您的帮助!

推荐答案

作为测试生命周期阶段的一部分执行的标准单元测试无法看到项目JAR,因为 test 阶段在执行之前执行 package 阶段,因此您的测试在Maven生成JAR之前运行。有关生命周期阶段及其列表,请参见此页面。订单。

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.

有很多教程可以设置Maven来运行集成测试。 此处这里是几个先发者。 故障安全插件通常用于执行集成测试。

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.

我不记得集成测试是否使用目标/类或项目的JAR文件类路径。但如果不是,您可以随时创建另一个Maven项目,在那里添加测试并将主项目添加为此集成测试项目的依赖项。在某些情况下,这可能比在主项目中使用集成测试阶段更可取,如果它不仅仅是标准的Java库,例如,如果您正在编写注释处理器。

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.

这篇关于执行Maven单元测试,并将classpath设置为生成的项目JAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆