Re: Adding @NonParameterized

David Saff <[email protected]> Mon, 27 Jun 2011 11:40:10 -0400
Newsgroups gmane.comp.java.junit.devel
Message-ID <[email protected]>
Good point.

Here's how I'm thinking about moving forward: we currently have @Rule,
which provides a way to override the rules for _running_ a method, for
_every @Test_ method in the class.  I'm considering a similar
construct, which will allow overriding:

1) Which methods trigger tests
2) How many tests per method
3) Validation of test methods
4) Test-instance creation
5) Test-method invoking

Given this, it should be possible to enable something very close to
@NonParameterized as a drop-in Rule-like object, rather than as a core
modification to the overall Parameterized runner.

One issue: I can't think of a name for this new construct.  Suggestions wel=
come.

   David Saff

On Sat, Jun 25, 2011 at 4:11 PM, Marius Kruger <[email protected]> wrote:
> On 25 June 2011 21:19, Marius Kruger <[email protected]> wrote:
>> On 20 June 2011 21:57, David Saff <[email protected]> wrote:
>> ...
>>> Parameterized.class currently allows one parameterization per test
>>> class. =A0This patch enables two parameterizations, one of which must be
>>> the special case of "no parameters". =A0A runner like JUnitParams
>>> (http://code.google.com/p/junitparams/) enables an arbitrary number of
>>> parameterizations, including "no parameters". =A0I'd like to consider
>>> how to handle the arbitrary case, like JUnitParams, and would rather
>>> not create a special case for "no parameters" that users would need to
>>> migrate to the new framework when it's ready.
>>>
>>> Does that make sense?
>>
>> Yes now that I looked at JUnitParams. It is awesome - much more than I
>> needed so far.
>
> I now found a case that was handled much nicer with my patched version:
> I just want to setUp(@Before) the tests according to a parameter. This
> is possible with the standard JUnit Parameterized runner and my
> version because we get the value in the constructor. eg.:
>
> A)=3D=3D
> @RunWith(JUnitParamsRunner.class)
> public class MyJUnitParamsTest {
>
> =A0 =A0protected AbstractHomeLayout homeLayout;
>
> =A0 =A0public void setupHomeLayout(int layoutVersion) {
> =A0 =A0 =A0 =A0homeLayout =3D init(layoutVersion);
> =A0 =A0}
>
> =A0 =A0protected Object[] getLayoutVersions() {
> =A0 =A0 =A0return new Object[][] { { 0 }, { 1 } };
> =A0 =A0}
>
> =A0 =A0@Test
> =A0 =A0@Parameters(method =3D "getLayoutVersions")
> =A0 =A0public void testParameterized(int layoutVersion) throws IOExceptio=
n {
> =A0 =A0 =A0 =A0setupHomeLayout(layoutVersion);
> =A0 =A0 =A0 =A0//test stuff
> =A0 =A0}
>
> =A0 =A0@Test
> =A0 =A0public void testNonParameterized() throws IOException {
> =A0 =A0 =A0 =A0//test stuff
> =A0 =A0}
>
> =A0 // 10 more tests like these
> }
> =3D=3D
>
> B)=3D=3D
> @RunWith(value =3D Parameterized.class)
> public class MyParameterizedTest {
>
> =A0 =A0int layoutVersion
> =A0 =A0public HomeLayoutTest(int layoutVersion) {
> =A0 =A0 =A0 =A0 this.layoutVersion =3D layoutVersion;
> =A0 =A0}
>
> =A0 =A0protected AbstractHomeLayout homeLayout;
>
> =A0 =A0@Before
> =A0 =A0public void setupHomeLayout() {
> =A0 =A0 =A0 =A0homeLayout =3D init(layoutVersion);
> =A0 =A0}
>
> =A0 =A0@Parameters
> =A0 =A0public static Collection getLayoutVersions() {
> =A0 =A0 =A0Object[][] data =3D new Object[][] { { 0 }, { 1 } };
> =A0 =A0 =A0return Arrays.asList(data);
> =A0 =A0}
>
> =A0 =A0@Test
> =A0 =A0public void testParameterized() throws IOException {
> =A0 =A0 =A0 =A0setupHomeLayout(layoutVersion);
> =A0 =A0 =A0 =A0//test stuff
> =A0 =A0}
>
> =A0 =A0@Test
> =A0 =A0@NonParameterized
> =A0 =A0public void testNonParameterized() throws IOException {
> =A0 =A0 =A0 =A0//test stuff
> =A0 =A0}
>
> =A0 // 10 more tests like these
> }
> =3D=3D
>
>
> So with A you have to modify three lines for every single parameterized t=
est:
> @Parameters(method =3D "getLayoutVersions")
> =A0 =A0public void testParameterized(int layoutVersion) throws IOExceptio=
n {
> =A0 =A0 =A0 =A0setupHomeLayout(layoutVersion);
> but in B you just annotate the non-parameterized tests...
>
>
>
> --
> <>< Marius ><>
>

---------------------------------------------------------------------------=
---
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security =

threats, fraudulent activity, and more. Splunk takes this data and makes =

sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2