如何用java在cucumber中传递布尔参数?

Lam*_*udi 7 java cucumber

如何使用java在cucumber中传递布尔参数?我有以下带有布尔参数的方法:

@Then("test if he is a admin")
public void verify_connect(boolean admin)
{
    if(admin)
        connectAsAdmin();
    else 
        connectAsUser();
}
Run Code Online (Sandbox Code Playgroud)

alb*_*lcs 13

使用@ParameterType https://cucumber.io/docs/cucumber/cucumber-expressions/

@ParameterType(value = "true|True|TRUE|false|False|FALSE")
public Boolean booleanValue(String value) {
    return Boolean.valueOf(value);
}

@Then("Something is set to {booleanValue}")
public void somethingIsSetTo(Boolean value) {

}
Run Code Online (Sandbox Code Playgroud)


Gre*_*rdt 2

无法将字符串“admin”转换为布尔值,但是您可以通过使用括号在正则表达式中创建反向引用来参数化用户类型:

@Then("test if he is a (admin|user)")
public void verify_connect(String userType) {
    if (userType == "admin")
        connectAsAdmin();
    else 
        connectAsUser();
}
Run Code Online (Sandbox Code Playgroud)