好吧,我知道这被认为是反模式,我当然愿意采取更好的方法.
Ok, I know this is considered an anti-pattern, and I am certainly open to a better way of doing this.
我有一个枚举值映射.我想确保将每个枚举值都分配给某个对象.我的测试看起来像这样.
I have a map of enum values. I want to ensure that each of those enum values is assigned to something. My test looks like this.
@Test public void eachRowRequiresCellCalc() { Model model = new Model(); EnumValues[] values = EnumValues.values(); for (EnumValues value : values) { Assert.assertTrue(String.format("%s must be assigned", value.name()), model.hasEnumValue(value)); } }这可以完成我要寻找的90%的内容.它没有做的是告诉我是否未分配多个枚举值(第一个失败).JUnit是否可以通过每次测试使多个失败?
This works and accomplishes 90% of what I'm looking for. What it doesn't do is show me if multiple enum values are unassigned (it fails on the first). Is there a way with JUnit to have multiple fails per test?
推荐答案使用 junit-快速检查为:
@RunWith(Theories.class) public class Models { @Theory public void mustHaveValue(@ForAll @ValuesOf EnumValues e) { assertTrue(e.name(), new Model().hasEnumValue(e)); } }这将为您的枚举的每个值运行理论方法.
This would run the theory method for every value of your enum.
另一种表达方式是通过参数化测试.
Another way to express this would be via a parameterized test.
(完全公开:我是junit-quickcheck的创建者.)
(Full disclosure: I am the creator of junit-quickcheck.)
更多推荐
一次测试即可获得多个结果
发布评论