如何使用 JUNIT 测试 ENUM
How to test ENUMs using JUNIT
如何使用 JUNIT 创建测试用例来测试 ENUMS 类型。下面我添加了枚举类型的代码。
public class TrafficProfileExtension {
public static enum CosProfileType {
BENIGN ("BENIGN"),
CUSTOMER ("CUSTOMER"),
FRAME ("FRAME"),
PPCO ("PPCO"),
STANDARD ("STANDARD"),
W_RED ("W-RED"),
LEGACY("LEGACY"),
OPTIONB ("OPTIONB");
private final String cosProfileType;
private CosProfileType(String s) {
cosProfileType = s;
}
public boolean equalsName(String otherName){
return (otherName == null)? false:cosProfileType.equals(otherName);
}
public String toString(){
return cosProfileType;
}
}
}
我为我的枚举 CosProfileType
创建了一个测试用例,但我在 CosProfileType.How 上遇到了一个错误,我可以让这个测试用例工作吗?
@Test
public void testAdd() {
TrafficProfileExtension ext = new TrafficProfileExtension();
assertEquals("FRAME", ext.CosProfileType.FRAME);
}
您正在将 String
与永远不相等的 Enum
进行比较。
尝试:
@Test
public void testAdd() {
TrafficProfileExtension ext = new TrafficProfileExtension();
assertEquals("FRAME", ext.CosProfileType.FRAME.toString());
}
由于 CosProfileType
被声明 public static
它实际上是一个顶级 class (枚举)所以你可以做
assertEquals("FRAME", CosProfileType.FRAME.name());
assertEquals("FRAME", CosProfileType.FRAME.name());
仅当字段和值都相同时才有效,但不适用于以下情况:
FRAME ("frame_value")
最好用
检查一下
assertEquals("FRAME", CosProfileType.FRAME.getFieldName());
如何使用 JUNIT 创建测试用例来测试 ENUMS 类型。下面我添加了枚举类型的代码。
public class TrafficProfileExtension {
public static enum CosProfileType {
BENIGN ("BENIGN"),
CUSTOMER ("CUSTOMER"),
FRAME ("FRAME"),
PPCO ("PPCO"),
STANDARD ("STANDARD"),
W_RED ("W-RED"),
LEGACY("LEGACY"),
OPTIONB ("OPTIONB");
private final String cosProfileType;
private CosProfileType(String s) {
cosProfileType = s;
}
public boolean equalsName(String otherName){
return (otherName == null)? false:cosProfileType.equals(otherName);
}
public String toString(){
return cosProfileType;
}
}
}
我为我的枚举 CosProfileType
创建了一个测试用例,但我在 CosProfileType.How 上遇到了一个错误,我可以让这个测试用例工作吗?
@Test
public void testAdd() {
TrafficProfileExtension ext = new TrafficProfileExtension();
assertEquals("FRAME", ext.CosProfileType.FRAME);
}
您正在将 String
与永远不相等的 Enum
进行比较。
尝试:
@Test
public void testAdd() {
TrafficProfileExtension ext = new TrafficProfileExtension();
assertEquals("FRAME", ext.CosProfileType.FRAME.toString());
}
由于 CosProfileType
被声明 public static
它实际上是一个顶级 class (枚举)所以你可以做
assertEquals("FRAME", CosProfileType.FRAME.name());
assertEquals("FRAME", CosProfileType.FRAME.name());
仅当字段和值都相同时才有效,但不适用于以下情况:
FRAME ("frame_value")
最好用
检查一下assertEquals("FRAME", CosProfileType.FRAME.getFieldName());