繁体   English   中英

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

[英]How to make test class read data from target/ and not target/test-classes Java

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

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

但是,当我执行mvn clean (同时清除目标文件夹和测试数据)然后执行mvn clean install我得到了测试失败。 因为测试正在尝试从target / test-classes / pdf-content而不是从target / pdf-content读取

如何使我的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>

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

对于静态资源,它位于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阶段进行声明)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM