Java 如何使用 JUNIT 测试 ENUM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32150174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to test ENUMs using JUNIT
提问by yesco1
How can I create test cases using JUNIT to test ENUMS types. Below I added my code with the enum type.
如何使用 JUNIT 创建测试用例来测试 ENUMS 类型。下面我用 enum 类型添加了我的代码。
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;
}
}
}
I created a test case for my enum CosProfileType
, and I am getting an error on CosProfileType.How can I make this test case work?
我为 enum 创建了一个测试用例CosProfileType
,但在 CosProfileType 上出现错误。如何使这个测试用例工作?
@Test
public void testAdd() {
TrafficProfileExtension ext = new TrafficProfileExtension();
assertEquals("FRAME", ext.CosProfileType.FRAME);
}
采纳答案by Reimeus
Since CosProfileType
is declared public static
it is effectively a top level class (enum) so you could do
由于CosProfileType
被声明public static
它实际上是一个顶级类(枚举)所以你可以做
assertEquals("FRAME", CosProfileType.FRAME.name());
回答by jbarrueta
You are comparing and String
to an Enum
that will never be equal.
您比较和String
一个Enum
永远不会是平等的。
Try:
尝试:
@Test
public void testAdd() {
TrafficProfileExtension ext = new TrafficProfileExtension();
assertEquals("FRAME", ext.CosProfileType.FRAME.toString());
}