如何使测试类从目标/而不是目标/测试类Java读取数据

时间:2016-02-29 15:00:36

标签: java maven junit

我有一个Java项目,当我从命令行构建mvn clean install命令时,它会生成一个输出目标文件夹。

我有一个JUnit测试类,它从这个目标文件夹中获取测试数据。直接运行此测试类(右键单击Testclass.java> Run As> Junit Test)工作正常,因为已生成目标文件夹。

然而,当我执行mvn clean(清除目标文件夹和我的测试数据)时,请执行mvn clean install我的测试失败。因为测试试图从目标/测试类/ pdf内容而不是目标/ pdf内容中读取

如何让我的Testclass从target/而不是target/test-classes读取测试数据?

编辑:

<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
    <execution>
        <id>unpack and rename content</id>
        <phase>process-resources</phase>
        <goals>
            <goal>execute</goal>
        </goals>
<configuration>
    <properties>
    </properties>
    <source>
        import com.cg.fs.common.FileUtil
        import com.cg.fs.common.ZipUtil
        import com.cg.fs.common.StringUtil

        com.services.fs.common.logging.LoggerFactory.setLogger("com.services.fs.common.logging.SystemOutLogger")
        com.services.fs.common.logging.FSLogger.setMinimal(true)
        buildHelper = new BuildHelper()

        output = new File("target")
        pdfContent = new File("target/pdf-content")

        // unzip tt intermediate thing
        List ttZip = FileUtil.discoverFiles(output, true, "production-content-.*\\.zip")
        assert ttZip.size() == 1, "Did not find expected number of content zip " + ttZip.size()
        println "Unzipping " + ttZip.get(0)
        ZipUtil.unzipToDisk(ttZip.get(0))

        // move it to pdf content
        List content = FileUtil.discoverDirectories(output, "production-content-.*-intermediate", true)
        assert tContent.size() == 1, "Did not find expected number of contents " + tContent.size()
        println "Moving " + tContent.get(0) + " to " + pdfContent
        success = FileUtil.copy(tContent.get(0), pdfContent)
        assert success, "Could not copy content"

        // move pdf content
        List fpsContent = FileUtil.discoverDirectories(output, “ls-production-content.*", true)
        assert fpsContent.size() == 1, "Did not find expected number of ls contents " + fpsContent.size()
        println "Moving " + fpsContent.get(0) + " to target/pdf-content"
        success = FileUtil.copy(fpsContent.get(0), pdfContent)
        assert success, "Could not copy pdf content"

        // rename to not be unsupported
        List unsupportedDirs = FileUtil.discoverDirectories(pdfContent, "_unsupported_.*", true)
        println "Renaming _unsupported content: " + unsupportedDirs
        for (File file : unsupportedDirs) {
        file.renameTo(new File(file.getParentFile(), file.getName().replace("_unsupported_", "")))
        }
    </source>
</configuration>

1 个答案:

答案 0 :(得分:1)

您有相反的问题:您不希望maven-surefire-plugintarget而非target/test-classes读取。您真正想要做的是将您的文件添加为项目的测试资源。

对于静态资源,这属于src/test/resources。默认情况下,maven-resources-plugin会将这些资源复制到target/test-classes

对于生成的资源,您可以使用build-helper-maven-plugin:add-test-resource目标将生成的内容添加为测试资源。如果它们是在target/pdf-content下生成的,则可以:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>1.10</version>
  <executions>
    <execution>
      <id>add-resource</id>
      <phase>generate-test-resources</phase>
      <goals>
        <goal>add-test-resource</goal>
      </goals>
      <configuration>
        <resources>
          <resource>
            <directory>${project.build.directory}/pdf-content</directory>
          </resource>
        </resources>
      </configuration>
    </execution>
  </executions>
</plugin>

这会将文件夹target/pdf-content下的所有文件添加为类路径根目录中的测试资源。如果他们需要位于指定的包中,您可以在配置中添加<targetPath>,它们将在指定的targetPath下可用。请注意,插件绑定到generate-test-resources阶段,因此您需要确保之前发生这些资源的生成(例如,通过在POM中使用阶段generate-test-resources声明之前)。