简体   繁体   中英

Cucumber feature cannot detect the relative path in eclipse

I have created a project in eclipse Kepler using cucumber and junit runner. Also I am using log4j.properties file placed under config folder in my project. Log4j property configuration is defined as below in Base class.

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 {

}

Now When i run the Profile.feature file in eclipse it shows -

java.io.FileNotFoundException: config\\log4j.properties (The system cannot find the path specified)

But when I directly run the RunnerTest.java file then it works perfect.

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'. 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.

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.

In Eclipse, right-click on the resources directory, and select Build Path > Use as Source Folder. 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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM