简体   繁体   English

在JUnit参数化测试中使用枚举的所有值

[英]Use all values of an enum in a JUnit parameterized test

I've found some examples how to use enum values in JUnit parameterized tests like this: 我发现了一些示例,该示例如何在JUnit参数化测试中使用枚举值,如下所示:

enum Colors {
  GREEN,
  RED,
  ORANGE
}

@Parameters
public static Collection<Object[]> data() {
  return Arrays.asList(new Object[][] {
    // How to use the complete enum here?
    {Colors.GREEN},
    {Colors.RED},
    {Colors.ORANGE},
  });
}

How can I use the complete enum in the return statement so that the test is run for each value of the enum? 如何在return语句中使用完整的枚举,以便针对枚举的每个值运行测试?

Use the values() method : 使用values()方法

Colors[] values = Colors.values();

List<Object[]> data = new ArrayList<>(values.length);
for (Colors color : values) { data.add(new Object[] { color }); }
return data;

Note that by normal convention the enum's name should be Color , not Colors : look at the enums in the standard library. 请注意,按照惯例,枚举的名称应为Color而不是Colors :请查看标准库中的枚举。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM