带格式的JUnit测试(String,String,Enum)

时间:2014-04-14 00:06:21

标签: java junit enums

我的教授要求JUnit Test采用这种格式

company = new Company("New Source");
    company.addEmployee("John", "Smith","Manufacturing");
    company.addEmployee("Bob", "Brown", "Manufacturing");
    company.addEmployee("Harold", "Jones", "Sales");
    company.addEmployee("Betty","Boop", "Design");

但问题是方法是这种格式

public String addEmployee(String fName, String lName, Position p)

并且必须是Position是枚举方法。那么如何才能使JUnit测试正常运行。顺便说一句,这就是我的枚举类看起来应该如何工作。

//in Position.java
public enum Position {
DESIGN("Design"),
MANUFACTURING("Manufacturing"),
SALES("Sales");

private final String positionName;


private Position(String positionName) {
    this.positionName= positionName;
}


@Override
public String toString() {
    return positionName;
}
}

1 个答案:

答案 0 :(得分:1)

如果您无法更改方法签名,则有两种选择:显式传递枚举值,或使用valueOf方法并使用与枚举值对应的字符串。

所以而不是

company = new Company("New Source");
company.addEmployee("John", "Smith","Manufacturing");

使用

company = new Company("New Source");
company.addEmployee("John", "Smith", Position.MANUFACTURING);

company = new Company("New Source");
company.addEmployee("John", "Smith", Position.valueOf("Manufacturing"));