我在eclipse Kepler中使用Cucumber和junit runner创建了一个项目。此外,我正在使用放置在项目中配置文件夹下的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"
跑步者测试.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
>srcmainjava
>srctestjava
>config > log4j.properties
我已经在日食中安装并下载了与黄瓜相关的所有插件。
将配置文件定义为资源:
如果你的项目是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());