[英]Cucumber feature cannot detect the relative path in eclipse
I have created a project in eclipse Kepler using cucumber and junit runner. 我已经使用黄瓜和junit运行器在eclipse Kepler中创建了一个项目。 Also I am using log4j.properties file placed under config folder in my project.
另外,我正在使用放在项目的config文件夹下的log4j.properties文件。 Log4j property configuration is defined as below in Base class.
Log4j属性配置在下面的基类中定义。
PropertyConfigurator.configure("config/log4j.properties");
Profile.feature 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 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 {
}
Now When i run the Profile.feature file in eclipse it shows - 现在,当我在Eclipse中运行Profile.feature文件时,它显示-
java.io.FileNotFoundException: config\\log4j.properties (The system cannot find the path specified)
java.io.FileNotFoundException:config \\ log4j.properties(系统找不到指定的路径)
But when I directly run the RunnerTest.java file then it works perfect. 但是当我直接运行RunnerTest.java文件时,它可以完美运行。
Why my feature file is not taking the relative path. 为什么我的特征文件没有采用相对路径。 It work when I hard code the file location like 'D:\\Project\\Workspace\\DemoCucumer\\config\\log4j.properties'.
当我对“ D:\\ Project \\ Workspace \\ DemoCucumer \\ config \\ log4j.properties”之类的文件位置进行硬编码时,它可以工作。 It not possible to change the file path every-time when location is changed.
位置更改后,不可能每次都更改文件路径。
Project Structure: 项目结构:
DemoCucumber
>src\main\java
>src\test\java
>config > log4j.properties
I have installed and downloaded all plugin related to cucumber in eclipse. 我已经在Eclipse中安装并下载了与黄瓜相关的所有插件。
You need to define your configuration file as a resource: 您需要将配置文件定义为资源:
If your project is a Maven project, you need to have a directory called src/main/resources
and move the entire "config" directory in there. 如果您的项目是Maven项目,则需要有一个名为
src/main/resources
目录,并将整个“ config”目录移到该目录中。
In Eclipse, right-click on the resources
directory, and select Build Path > Use as Source Folder. 在Eclipse中,右键单击
resources
目录,然后选择“构建路径”>“用作源文件夹”。 The decorator on the folder icon will change! 文件夹图标上的装饰器将更改!
In your code, to retrieve the file, you need something like: 在代码中,要检索文件,您需要类似以下内容:
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.