Java 验证枚举值只发生一次
我有一个枚举和一个对象,我想在junit测试中验证其唯一性 例如,我有一个枚举颜色,如下所示:Java 验证枚举值只发生一次,java,arraylist,junit,enums,Java,Arraylist,Junit,Enums,我有一个枚举和一个对象,我想在junit测试中验证其唯一性 例如,我有一个枚举颜色,如下所示: public enum Colors{ Yellow("This is my favorite color"), Blue("This color is okay"), Orange("I do not like this color"), Green("I hate this color"); private String value; Col
public enum Colors{
Yellow("This is my favorite color"),
Blue("This color is okay"),
Orange("I do not like this color"),
Green("I hate this color");
private String value;
Colors(String value) {
this.value = value;
}
public String getDescription() {
return value;
}
}
我还有一个名为ColorList的ArrayList,它包含具有两个属性的颜色对象:value和description。我想验证ColorList以测试是否有四个颜色对象包含枚举中的值。我希望我的测试失败,如果:
请检查以下junit测试用例,这些测试用例将在指定的情况下失败
import java.util.ArrayList;
import java.util.List;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
enum Colors {
Yellow("This is my favorite color"), Blue("This color is okay"), Orange(
"I do not like this color"), Green("I hate this color");
String value;
Colors(String value) {
this.value = value;
}
public String getDescription() {
return value;
}
}
public class JunitSample {
private List<Colors> smallList;
private List<String> largeList;
@Before
public void setUp() {
smallList = new ArrayList<Colors>();
largeList = new ArrayList<String>();
// Not keeping yellow in smallList.
smallList.add(Colors.Blue);
smallList.add(Colors.Green);
smallList.add(Colors.Orange);
largeList.add("Blue");
largeList.add("Green");
largeList.add("Orange");
largeList.add("Yellow");
largeList.add("Red"); // Red is not defined in Colors Enum class
}
@Test
public void testColorsWhichAreNotThereInEnum() {
for(String value : largeList){
Assert.assertNotNull("value not available", Colors.valueOf(value));
}
}
@Test
public void testColorsWhichAreNotThereInSmallList() {
for(Colors color : Colors.values()){
Assert.assertEquals("Color not availale in list",true, smallList.contains(color));
}
}
}
import java.util.ArrayList;
导入java.util.List;
导入junit.framework.Assert;
导入org.junit.Before;
导入org.junit.Test;
枚举颜色{
黄色(“这是我最喜欢的颜色”)、蓝色(“这颜色不错”)、橙色(
“我不喜欢这个颜色”)、绿色(“我讨厌这个颜色”);
字符串值;
颜色(字符串值){
这个值=值;
}
公共字符串getDescription(){
返回值;
}
}
公共类JunitSample{
私人名单;
私人名单诈骗者;
@以前
公共作废设置(){
smallList=newarraylist();
largeList=newarraylist();
//不在小名单中保留黄色。
小列表。添加(颜色。蓝色);
小列表。添加(颜色。绿色);
小列表。添加(颜色。橙色);
largeList.添加(“蓝色”);
largeList.添加(“绿色”);
添加(“橙色”);
largeList.添加(“黄色”);
largeList.add(“Red”);//颜色枚举类中未定义红色
}
@试验
公共无效测试颜色,其颜色为{
for(字符串值:largeList){
Assert.assertNotNull(“值不可用”,Colors.valueOf(值));
}
}
@试验
public void testColorsWhichAreNotThereInSmallList(){
对于(颜色:Colors.values()){
Assert.assertEquals(“颜色在列表中不可用”,true,smallList.contains(颜色));
}
}
}
请检查以下junit测试用例,这些测试用例在指定的情况下将失败
import java.util.ArrayList;
import java.util.List;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
enum Colors {
Yellow("This is my favorite color"), Blue("This color is okay"), Orange(
"I do not like this color"), Green("I hate this color");
String value;
Colors(String value) {
this.value = value;
}
public String getDescription() {
return value;
}
}
public class JunitSample {
private List<Colors> smallList;
private List<String> largeList;
@Before
public void setUp() {
smallList = new ArrayList<Colors>();
largeList = new ArrayList<String>();
// Not keeping yellow in smallList.
smallList.add(Colors.Blue);
smallList.add(Colors.Green);
smallList.add(Colors.Orange);
largeList.add("Blue");
largeList.add("Green");
largeList.add("Orange");
largeList.add("Yellow");
largeList.add("Red"); // Red is not defined in Colors Enum class
}
@Test
public void testColorsWhichAreNotThereInEnum() {
for(String value : largeList){
Assert.assertNotNull("value not available", Colors.valueOf(value));
}
}
@Test
public void testColorsWhichAreNotThereInSmallList() {
for(Colors color : Colors.values()){
Assert.assertEquals("Color not availale in list",true, smallList.contains(color));
}
}
}
import java.util.ArrayList;
导入java.util.List;
导入junit.framework.Assert;
导入org.junit.Before;
导入org.junit.Test;
枚举颜色{
黄色(“这是我最喜欢的颜色”)、蓝色(“这颜色不错”)、橙色(
“我不喜欢这个颜色”)、绿色(“我讨厌这个颜色”);
字符串值;
颜色(字符串值){
这个值=值;
}
公共字符串getDescription(){
返回值;
}
}
公共类JunitSample{
私人名单;
私人名单诈骗者;
@以前
公共作废设置(){
smallList=newarraylist();
largeList=newarraylist();
//不在小名单中保留黄色。
小列表。添加(颜色。蓝色);
小列表。添加(颜色。绿色);
小列表。添加(颜色。橙色);
largeList.添加(“蓝色”);
largeList.添加(“绿色”);
添加(“橙色”);
largeList.添加(“黄色”);
largeList.add(“Red”);//颜色枚举类中未定义红色
}
@试验
公共无效测试颜色,其颜色为{
for(字符串值:largeList){
Assert.assertNotNull(“值不可用”,Colors.valueOf(值));
}
}
@试验
public void testColorsWhichAreNotThereInSmallList(){
对于(颜色:Colors.values()){
Assert.assertEquals(“颜色在列表中不可用”,true,smallList.contains(颜色));
}
}
}
我认为使用枚举集
可以做你最想做的事情。这将确保您拥有所有颜色一次且仅一次,除此之外没有其他颜色
EnumSet<Colors> allColors = EnumSet.allOf(Colors.class);
顺便说一下,我在Eclipse中遇到了一个编译错误:枚举值列表以逗号而不是分号结尾
另外,在一些样式方面,我不知道您是否能够更改枚举,但是,如果可以,Java中的常规约定是在
所有的_CAPS
中使用枚举值,并使枚举类名为单数(而不是复数)-例如,您可以将其称为public enum NamedColor{YELLOW,RED;}
。您还可以将值
重命名为说明
,以使其用途更清楚。我认为您可以使用枚举集
做您最想做的事情。这将确保您拥有所有颜色一次且仅一次,除此之外没有其他颜色
EnumSet<Colors> allColors = EnumSet.allOf(Colors.class);
顺便说一下,我在Eclipse中遇到了一个编译错误:枚举值列表以逗号而不是分号结尾
另外,在一些样式方面,我不知道您是否能够更改枚举,但是,如果可以,Java中的常规约定是在
所有的_CAPS
中使用枚举值,并使枚举类名为单数(而不是复数)-例如,您可以将其称为public enum NamedColor{YELLOW,RED;}
。您还可以将值
重命名为说明
,以使其用途更清楚。是否必须使用列表
?你可以用它来代替;这将保证集合中每个枚举值只有一个。如果需要包含所有值,可以构造一个到一个。我相信使用枚举集需要从java.lang.Enum扩展颜色,但是我不能在这里修改颜色case@DevelopingDeveloper根据:所有枚举隐式扩展java.lang.Enum
——Java语言规范说:枚举类型E的直接超类是enum
@DevelopingDeveloper,EnumSet
的目的是容纳enum
s,因此您可以这样使用它。你能用集合来代替吗?它通常比List
@DevelopingDeveloper更符合枚举的逻辑集合,“需要从java.lang.Enum扩展颜色”?但它是一个枚举
!所以问题是…?你必须使用列表吗?你