2016-02-29 57 views
1

我有一个Java项目,当我从命令行构建mvn clean install命令时,它会生成一个输出目标文件夹。如何使测试类从目标/而不是目标/测试类读取数据Java

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

但是当我做一个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

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

对于静态资源,这是根据src/test/resourcesmaven-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下的所有文件,在classpath的根目录中的测试资源。如果他们需要使用指定的软件包,则可以将<targetPath>添加到配置中,并且它们将在指定的targetPath下提供。请注意,该插件绑定到generate-test-resources阶段,因此您需要确保这些资源的生成发生在之前(例如,通过在POM中声明阶段generate-test-resources)。

+0

我与maven斗争,我避免搞乱我的pom。因此,在'<! - 放置测试资源的路径 - >'我究竟会放什么? SRC /测试/资源? –

+1

@arabian_albert好的,我编辑了。你可以离开它。如果您想要指定包中的资源,则只需要它。默认情况下,它将位于类路径的根目录下。 – Tunaki

+0

说我确实希望它在指定的包中,我会把什么放在targetPath下?我想拥抱maven我只是不认为我理解正确 –