0

I'm trying to write some steps in Cucumber-jvm and I've got a step that is approximately like the following:

@Given("I am a (regular|admin|guest) user")
 public void setUser(String userType){
     if("regular".equals(userType))
         setUserType(REGULAR);
     if("admin".equals(userType))
         setUserType(ADMIN);
     if("guest".equals(userType))
         setUserType(GUEST);
}

Is there a better way of writing such a step, to make it polymorphic?

4

1 回答 1

0

在某些时候,您需要在字符串和要设置的类型之间建立关系,所以如果您不想要这样的大 if statmenet,那么设置映射可能是要走的路。

这样的事情怎么办......

public static Map<String,UserType> types = new HashMap<String,UserType>();
static {
  types.put("regular", REGULAR);
  types.put("admin", ADMIN);
  types.put("guest", GUEST);
}

接着

@Given("I am a (regular|admin|guest) user")
public void setUser(String userType){
 setUserType(types.get(userType));
}
于 2013-05-13T19:22:48.780 回答