2016-04-28 72 views

回答

1

這並不完全清楚這些場景的步驟定義是否在單獨的類中,但我認爲它們是並且ScenarioA中的步驟已執行一個在B.

public class ScenarioA { 

    public static String getVariableYouWantToUse() { 
    return variableYouWantToUse; 
    } 

    private static String variableYouWantToUse; 

    Given("your step that expects one parameter") 
    public void some_step(String myVariable) 
    variableYouWantToUse = myVariable; 
} 

然後在場景B.

public class ScenarioB { 

    Given("other step") 
    public void some_other_step() 
    ScenarioA.getVariableYouWantToUse(); 
} 
-1

只是爲了記錄在案,而不是依靠靜態前一個可以用黃瓜JVM的Dependency Injection功能。

0

正如@Mykola所提到的,最好的方法是使用依賴注入。爲了給一個簡單的解決方案使用手動依賴注入,你可以做類似於

public class OneStepDefinition{ 

private String user; 

// and some setter which could be your step definition methods 

public String getUser() { 
    return user; 
} 

} 

public class AnotherStepDefinition { 

private final OneStepDefinition oneStepDefinition; 

public AnotherStepDefinition(OneStepDefinition oneStepDefinition) { 
     this.oneStepDefinition = oneStepDefinition; 
    } 

// Some step defs 
@Given("^I do something on the user created in OneStepDefinition class$") 
    public void doSomething() { 
    String user = oneStepDefinition.getUser(); 
// do something with the user 
    } 
}