在我的单元测试中,我想在$ {project.build.directory}中创建一个tmp目录.如何在单元测试中访问$ {project.build.directory}的值?
我能想到的一种方法是在测试资源中提供过滤的属性文件,该文件保留该值.(我还没有尝试,但我认为这应该有效.)
是否有直接访问/传递此属性值的方法?
此致,弗洛里安
Upg*_*ave 18
我曾经使用过这样的东西并取得了一些成功.即使不使用Maven,单元测试仍将运行,目标目录仍将相对于运行测试的cwd创建两个目录.
public File targetDir(){
String relPath = getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
File targetDir = new File(relPath+"../../target");
if(!targetDir.exists()) {
targetDir.mkdir();
}
return targetDir;
}
Run Code Online (Sandbox Code Playgroud)
Adr*_*nRM 14
我认为如果你按照http://maven.apache.org/plugins/maven-surefire-plugin/examples/system-properties.html所解释的那样配置surefire-plugin,使用系统属性是非常简单的.即便是直接回答你问题的例子:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<systemPropertyVariables>
<propertyName>propertyValue</propertyName>
<buildDirectory>${project.build.directory}</buildDirectory>
[...]
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
Run Code Online (Sandbox Code Playgroud)
请记住,您的单元测试不必从Maven surefire插件执行,因此${project.build.directory}可能无法使用属性.为了使您的测试更便携,我宁愿推荐使用File.createTempFile().
| 归档时间: |
|
| 查看次数: |
14268 次 |
| 最近记录: |