Re: JUnit 4.12 issue when using theory + enum type + DataPoint that contains a null
David Saff <[email protected]> Fri, 3 May 2013 10:03:16 -0700
| Newsgroups | gmane.comp.java.junit.user |
|---|---|
| Message-ID | <CALrw-Py+qa2vgWvAeBuA5xB0VkRB8_JhKcA851mSOHe-QW=KNw@mail.gmail.com> |
Will, I think this is relevant to recent work on theories by @pimterry. Can you create a bug on github, and mention @pimterry in the description? Thanks, David On Wed, May 1, 2013 at 5:59 PM, willmcqueen <[email protected]> wrote: > ** > > > Hi, > > I have a Theory with a parameter list that includes a String, a boolean, > and an enum. The latter 2 should be automatically assigned and should not > need any explicit @DataPoint declaration (per the release note description > below. When I run the test, the enum param is assigned the null value > instead of being assigned each value of the enum, and I'm not sure why this > is not working for me. I would appreciate any help you can offer, please. > > I cloned master from here: > > https://github.com/junit-team/junit > > ...and looked at the JUnit 4.12 release notes: > > https://github.com/junit-team/junit/wiki/4.12-release-notes > > ...and scrolled-down to pull request #654 which says: > > "Any theory method parameters with boolean or enum types that can't be > supplied with values by any other sources will be automatically supplied > with default values: true and false, or every value of the given enum. If > other explicitly defined values are available (e.g. from a specified > ParameterSupplier or some DataPoints method in the theory class), only > those explicitly defined values will be used." > > On that same release notes page, I also see the comment under pull request > #549, which says that a @DataPoint-annotated array field can contain null > values. > > So here's my test: > > import org.junit.experimental.theories.DataPoints; > import org.junit.experimental.theories.Theories; > import org.junit.experimental.theories.Theory; > import org.junit.runner.RunWith; > > @RunWith(Theories.class) > public class QuickTest { > > @DataPoints > public static String[] platforms = new String[]{"windows", "linux", null}; > > public static enum JDKS { > JDK6, > JDK7, > } > > @Theory > public void testFoo(String platform, boolean truth, JDKS j) throws > Exception { > System.out.println(platform + ":" + truth + ":" + j); > } > > } > > ...and here's the output: > windows:true:null > windows:false:null > linux:true:null > linux:false:null > null:true:null > null:false:null > > ...which is not what I expected... I expected the 3rd value to be JDK6 or > JDK7. I run a 2nd test, but this time I remove the null array element from > the 'platforms' field, and the results are expected: > > import org.junit.experimental.theories.DataPoints; > import org.junit.experimental.theories.Theories; > import org.junit.experimental.theories.Theory; > import org.junit.runner.RunWith; > > @RunWith(Theories.class) > public class QuickTest { > > @DataPoints > public static String[] platforms = new String[]{"windows", "linux"}; > > public static enum JDKS { > JDK6, > JDK7, > } > > @Theory > public void testFoo(String platform, boolean truth, JDKS j) throws > Exception { > System.out.println(platform + ":" + truth + ":" + j); > } > > } > > ...which results in: > windows:true:JDK6 > windows:true:JDK7 > windows:false:JDK6 > windows:false:JDK7 > linux:true:JDK6 > linux:true:JDK7 > linux:false:JDK6 > linux:false:JDK7 > > Cool. But now just for fun, I remove the 'platform' param from testFoo and > I restore the null array element to 'platforms' field. Since there is no > longer any String param in the param list (and since String is a final > class so there's no possibility of an assignable subclass), I expect that > the 'platforms' DataPoint will just be silently ignored and I would get the > expected output. But I actually see the unexpected output, as if a null > array element that exists in a field (that's not even used in the Theory's > param list) is affecting the values of a totally different type, the enum > type (causing the enum param to receive only a null value): > > import org.junit.experimental.theories.DataPoints; > import org.junit.experimental.theories.Theories; > import org.junit.experimental.theories.Theory; > import org.junit.runner.RunWith; > > @RunWith(Theories.class) > public class QuickTest { > > @DataPoints > public static String[] platforms = new String[]{"windows", "linux", null}; > > public static enum JDKS { > JDK6, > JDK7, > } > > @Theory > public void testFoo(boolean truth, JDKS j) throws Exception { > System.out.println(truth + ":" + j); > } > > } > > ...which results in: > true:null > false:null > > Maybe I'm missing something? > > Thank you. > > Cheers, > Will > > > [Non-text portions of this message have been removed] ------------------------------------ Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/junit/ <*> Your email settings: Individual Email | Traditional <*> To change settings online go to: http://groups.yahoo.com/group/junit/join (Yahoo! ID required) <*> To change settings via email: [email protected] [email protected] <*> To unsubscribe from this group, send an email to: [email protected] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/