[英]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.