2017-05-15 37 views
0

我有一个枚举和对象,我想在junit测试中验证uniqeness。验证枚举值出现一次,只有一次

例如,我有一个枚举颜色,如下图所示:

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,它包含色彩具有两个属性的对象:值和说明。我想验证ColorList以测试在枚举中包含四个Color对象的值。我想我的测试失败如果任:

  1. ,是不是,是不是内枚举
+1

你必须使用'List'吗?您可以使用['EnumSet '](https://docs.oracle.com/javase/7/docs/api/java/util/EnumSet.html)。这可以保证集合中只有一个枚举值。如果你需要包含所有值,你可以通过['EnumSet.allOf(Colors.class)'](https://docs.oracle.com/javase/7/docs/api/java/util/EnumSet html的#allOf(java.lang.Class中))。 –

+0

我相信使用枚举集要求将颜色从java.lang.Enum进行扩展,但在这种情况下,我无法修改颜色 – DevelopingDeveloper

+1

@DevelopingDeveloper根据[Java™教程 - 枚举类型](https://docs.oracle。 com/javase/tutorial/java/javaOO/enum.html):*所有枚举隐式扩展java.lang.Enum *。* Java语言规范[§8.9。枚举类型](https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.9)说:*枚举类型E的直接超类是'Enum ' * – Andreas

回答

1

我认为你可以用EnumSet做你最想做的事。这将确保您只有一次所有的颜色,除此之外没有别的。

EnumSet<Colors> allColors = EnumSet.allOf(Colors.class); 

这里是我玩耍的情况下,它可以帮助:

import static org.hamcrest.Matchers.*; 
import static org.junit.Assert.*; 

import java.util.EnumSet; 

import org.junit.Test; 

public class TempTest { 

    @Test 
    public void x() { 
     EnumSet<Colors> allColors = EnumSet.allOf(Colors.class); 
     assertEquals(4, allColors.size()); 
     assertThat(allColors, contains(Colors.Yellow, Colors.Blue, Colors.Orange, Colors.Green)); 
     for (Colors c : allColors) { 
      System.out.println(c.name() + " (" + c.getDescription() + ")"); 
     } 
    } 
} 

它得到一个绿色的酒吧和打印:

Yellow (This is my favorite color) 
Blue (This color is okay) 
Orange (I do not like this color) 
Green (I hate this color) 

顺便说一句,我只好在Eclipse编译错误:您的枚举值列表以逗号代替分号结束。

此外,在几个样式点上,我不知道是否可以更改枚举,但是,如果可以,Java中的常规约定是枚举值为ALL_CAPS并使枚举类名单数(而不是复数)—例如你可以称它为public enum NamedColor { YELLOW, RED; }。您也可以将value重命名为description以使其目的更清晰。

1

在ArrayList中存在

  • 值在ArrayList中枚举中存在的值,请请检查以下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)); 
         } 
    
        } 
    
    }