JUnit 理论:为什么我不能使用列表(而不是数组)作为数据点?

发布于 2024-09-04 16:14:02 字数 1215 浏览 2 评论 0 原文

我已经开始使用新的 JUnit 理论 用于参数化测试的功能。例如,如果您的理论设置为采用 Integer 参数,则 Theories 测试运行程序会选取任何标有 Integer 的>@DataPoint:

@DataPoint
public static Integer number = 0;

以及数组中的任何Integer

@DataPoints
public static Integer[] numbers = {1, 2, 3};

甚至是返回数组的方法,例如:

@DataPoints 
public static Integer[] moreNumbers() { return new Integer[] {4, 5, 6}; };

但不在List中。以下不起作用:

@DataPoints 
public static List<Integer> numberList = Arrays.asList(7, 8, 9);

编辑:看起来其他集合也不支持,因为这不起作用。

@DataPoints 
public static Collection<Integer> numberList = new HashSet<Integer>() {{
  add(7);
  add(8);
  add(9);
}};

我是否做错了什么,或者 ListSet 等确实不起作用?不允许使用集合作为数据点是一个有意识的设计选择,还是这只是一个尚未实现的功能?是否有计划在 JUnit 的未来版本中实现它?

(我目前使用的是版本 4.8.1,而最新版本是 4.8.2,但是 看起来这不是4.8.2中添加的东西)

I've started using the new(ish) JUnit Theories feature for parameterizing tests. If your Theory is set up to take, for example, an Integer argument, the Theories test runner picks up any Integers marked with @DataPoint:

@DataPoint
public static Integer number = 0;

as well as any Integers in arrays:

@DataPoints
public static Integer[] numbers = {1, 2, 3};

or even methods that return arrays like:

@DataPoints 
public static Integer[] moreNumbers() { return new Integer[] {4, 5, 6}; };

but not in Lists. The following does not work:

@DataPoints 
public static List<Integer> numberList = Arrays.asList(7, 8, 9);

Edit: It looks like other collections are not supported either, as this does not work.

@DataPoints 
public static Collection<Integer> numberList = new HashSet<Integer>() {{
  add(7);
  add(8);
  add(9);
}};

Am I doing something wrong, or do Lists, Sets, etc. really not work? Was it a conscious design choice not to allow the use of Collections as data points, or is that just a feature that hasn't been implemented yet? Are there plans to implement it in a future version of JUnit?

(I'm currently using version 4.8.1 whereas the newest version is 4.8.2 but it looks like this is not something that was added in 4.8.2)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆光飞翔i 2024-09-11 16:14:02

我已经查看了这个问题,似乎现在有一个待提交的提交。它不在那里的原因似乎很简单,没有人要求它,而且做起来相当复杂(正如您在 你的补丁

I've looked at the issue, and it seems there is now a pending commit for it. The reason that it wasn't in there seems to be simply that nobody asked for it and it's quite complex to do (as you've proven in your patch)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文