[英]Cucumber feature cannot detect the relative path in eclipse
我已经使用黄瓜和junit运行器在eclipse Kepler中创建了一个项目。 另外,我正在使用放在项目的config文件夹下的log4j.properties文件。 Log4j属性配置在下面的基类中定义。
PropertyConfigurator.configure("config/log4j.properties");
Profile.feature
Feature: This is a profile feature
@Testing
Scenario: this is the first scenario for Profile page
Given I open Naukri app
When I tap on "Profile Image"
Then The toolbar shows "User Profile"
RunnerTest.java
@RunWith(Cucumber.class)
@CucumberOptions(
format = { "pretty", "html:target/html/", "json:target/json/output.json"},
features = "src/test/features",
glue = "com.stepsdefination",
tags = {"@Testing"}
)
public class RunnerTest {
}
现在,当我在Eclipse中运行Profile.feature文件时,它显示-
java.io.FileNotFoundException:config \\ log4j.properties(系统找不到指定的路径)
但是当我直接运行RunnerTest.java文件时,它可以完美运行。
为什么我的特征文件没有采用相对路径。 当我对“ D:\\ Project \\ Workspace \\ DemoCucumer \\ config \\ log4j.properties”之类的文件位置进行硬编码时,它可以工作。 位置更改后,不可能每次都更改文件路径。
项目结构:
DemoCucumber
>src\main\java
>src\test\java
>config > log4j.properties
我已经在Eclipse中安装并下载了与黄瓜相关的所有插件。
您需要将配置文件定义为资源:
如果您的项目是Maven项目,则需要有一个名为src/main/resources
目录,并将整个“ config”目录移到该目录中。
在Eclipse中,右键单击resources
目录,然后选择“构建路径”>“用作源文件夹”。 文件夹图标上的装饰器将更改!
在代码中,要检索文件,您需要类似以下内容:
URL res = getClass().getResource("/config/log4j.properties");
Assert.assertNotNull("Your resources are not configured correctly!", res);
File f = new File(res.getFile());
PropertyConfigurator.configure(f.getCanonicalPath());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.