11

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)

Tyler
  • 21,762
  • 11
  • 61
  • 90
  • As a side-note, I don't see a tag for this "Theories" feature. I assume the [`theory`] tag is for theoretical questions about things like big-O complexity and such. Please feel free to retag. – Tyler Jun 03 '10 at 16:32
  • +1 Seems quite odd. Did you try any other Collection types? – ponzao Jun 03 '10 at 19:21
  • Just tried using a `Set` and updated the question. That doesn't work either, whether you declare it as `Set` or `Collection`. – Tyler Jun 03 '10 at 20:22
  • I've added this as an issue: http://github.com/KentBeck/junit/issues/issue/110 I'll keep an eye on that issue and see if anyone posts a comment with some more information about this. – Tyler Jun 03 '10 at 22:21

1 Answers1

1

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)

iwein
  • 25,788
  • 10
  • 70
  • 111
  • 1
    Thanks for the follow-up. I should have added a comment to this SO answer saying that I did manage to figure out a solution (though it's certainly not perfect) and then sent a pull request on github. – Tyler Jun 23 '10 at 03:05
  • I was not sure, if putting a comment in 3 years old post is a good thing. But I just wish to know, whats the update on this issue. This issue is still not resolved in latest Mockito version. Also link to issue mentioend above does not exists anymore. – user613114 Jan 06 '14 at 05:44