Search the web
Sign In
New User? Sign Up
junit · JUnit, the Java unit testing framework written by Kent Beck and Erich Gamma.
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Show off your group to the world. Share a photo of your group with us.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
UnExplained Behavior Executing Parameteric Tests with JUnit 4.6   Topic List   < Prev Topic  |  Next Topic >
Summarize Messages Sort by Date  
#21796 From: "kartik_krishnanand" <kartik_krishnanand@...>
Date: Fri Jul 3, 2009 10:27 pm
Subject: UnExplained Behavior Executing Parameteric Tests with JUnit 4.6
kartik_krish...
Offline Offline
Send Email Send Email
 
Hi,

I tried to generate parameteric tests with JUnit 4.6. I don't know if this is an
expected behavior.

Test Code


@RunWith(Parameterized.class)
public class JUnit4Test {

private String name;

@BeforeClass
public static void setUpOnceBeforeClass() throws Exception {
System.out.println("Before Class");
}

@Before
public void setUp() throws Exception {
System.out.println("Before Method");
}

@After
public void tearDown() throws Exception {
System.out.println("After Method");
}

@AfterClass
public static void tearDownOnceAfterClass() throws Exception {
System.out.println("After Class");
}

@Test()
public void testOne() throws Exception {
System.out.println("One");
}

@Test
public void testTwo() throws Exception {
System.out.println("Two");
}

@Test
public void testThree() throws Exception {
System.out.println(this.name);
}

public JUnit4Test(String name) {
this.name = name;
}

@Parameters
public static Collection<Object[]> getNames() {
return Arrays.asList(new Object[][] {
{"Kartik"}, {"Rakesh"}, {"Ed"}
});
}
}

My output

Before Class
Before Method
One
After Method
Before Method
Two
After Method


Before Method
Kartik
After Method
Before Method
One
After Method
Before Method
Two
After Method
Before Method
Rakesh
After Method
Before Method
One
After Method
Before Method
Two
After Method
Before Method
Ed
After Method
After Class

It runs the unit tests three times. The testOne, testTwo outputs remain
unchanged, the testThree output changes with the depending upon the name.

Is this the expected behavior? I expected that testThree would run three times
each with a different parameter. Does anyone know if there is a reason for this
implementation.








#21798 From: Mike Forsberg <bigmike@...>
Date: Sat Jul 4, 2009 3:02 am
Subject: Re: UnExplained Behavior Executing Parameteric Tests with JUnit 4.6
bigmike_f
Offline Offline
Send Email Send Email
 
I've modified the program by adding a counting variable that is scoped
to the class. You can play arround with why this is important on your
own time... but anyway.

I got test's three's output changing correctly. Don't know what you
were expecting. If you would explain that we might be able to help
with this question more.

Here is my output...

Before Class
.Before Method
One
After Method
.Before Method
Two
After Method
.Before Method
count:0 Kartik
After Method
.Before Method
One
After Method
.Before Method
Two
After Method
.Before Method
count:1 Rakesh
After Method
.Before Method
One
After Method
.Before Method
Two
After Method
.Before Method
count:2 Ed
After Method
After Class

My testThree is
@Test
public void testThree() throws Exception {
System.out.println("count:" + count + " " + this.name);
count++;
}

I've added a private static int count to the class initialized to 0.
As you can see.

Hope I helped...

Big Mike

On Fri, Jul 3, 2009 at 5:27 PM,
kartik_krishnanand<kartik_krishnanand@...> wrote:
> Hi,
>
> I tried to generate parameteric tests with JUnit 4.6. I don't know if this is
an expected behavior.
>
> Test Code
>
>
> @RunWith(Parameterized.class)
> public class JUnit4Test {
>
>        private String name;
>
>        @BeforeClass
>        public static void setUpOnceBeforeClass() throws Exception {
>                System.out.println("Before Class");
>        }
>
>        @Before
>        public void setUp() throws Exception {
>                System.out.println("Before Method");
>        }
>
>        @After
>        public void tearDown() throws Exception {
>                System.out.println("After Method");
>        }
>
>        @AfterClass
>        public static void tearDownOnceAfterClass() throws Exception {
>                System.out.println("After Class");
>        }
>
>        @Test()
>        public void testOne() throws Exception {
>                System.out.println("One");
>        }
>
>        @Test
>        public void testTwo() throws Exception {
>                System.out.println("Two");
>        }
>
>        @Test
>        public void testThree() throws Exception {
>                System.out.println(this.name);
>        }
>
>        public JUnit4Test(String name) {
>                this.name = name;
>        }
>
>        @Parameters
>        public static Collection<Object[]> getNames() {
>                return Arrays.asList(new Object[][] {
>                                {"Kartik"}, {"Rakesh"}, {"Ed"}
>                });
>        }
> }
>
> My output
>
> Before Class
> Before Method
> One
> After Method
> Before Method
> Two
> After Method
>
>
> Before Method
> Kartik
> After Method
> Before Method
> One
> After Method
> Before Method
> Two
> After Method
> Before Method
> Rakesh
> After Method
> Before Method
> One
> After Method
> Before Method
> Two
> After Method
> Before Method
> Ed
> After Method
> After Class
>
> It runs the unit tests three times. The testOne, testTwo outputs remain
unchanged, the testThree output changes with the depending upon the name.
>
> Is this the expected behavior? I expected that testThree would run three times
each with a different parameter. Does anyone know if there is a reason for this
implementation.
>
>
>
>
>
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>
>



#21799 From: Puneet Lakhina <puneet.lakhina@...>
Date: Sat Jul 4, 2009 2:20 am
Subject: Re: UnExplained Behavior Executing Parameteric Tests with JUnit 4.6
mnferrari2000
Offline Offline
Send Email Send Email
 
It seems like this is intended. The documentation here:
http://junit.org/apidocs/org/junit/runners/Parameterized.html
When running a parameterized test class, instances are created for the
cross-product of the test methods and the test data elements.

If you think about it, how would junit figure out which are your instances
that require the parameters? I mean theres no way to figure out based on the
testThree method that this is the one that should be run with different
parameters.

Seems allright to me.

On Fri, Jul 3, 2009 at 15:27, kartik_krishnanand <
kartik_krishnanand@...> wrote:

>
>
> Hi,
>
> I tried to generate parameteric tests with JUnit 4.6. I don't know if this
> is an expected behavior.
>
> Test Code
>
> @RunWith(Parameterized.class)
> public class JUnit4Test {
>
> private String name;
>
> @BeforeClass
> public static void setUpOnceBeforeClass() throws Exception {
> System.out.println("Before Class");
> }
>
> @Before
> public void setUp() throws Exception {
> System.out.println("Before Method");
> }
>
> @After
> public void tearDown() throws Exception {
> System.out.println("After Method");
> }
>
> @AfterClass
> public static void tearDownOnceAfterClass() throws Exception {
> System.out.println("After Class");
> }
>
> @Test()
> public void testOne() throws Exception {
> System.out.println("One");
> }
>
> @Test
> public void testTwo() throws Exception {
> System.out.println("Two");
> }
>
> @Test
> public void testThree() throws Exception {
> System.out.println(this.name);
> }
>
> public JUnit4Test(String name) {
> this.name = name;
> }
>
> @Parameters
> public static Collection<Object[]> getNames() {
> return Arrays.asList(new Object[][] {
> {"Kartik"}, {"Rakesh"}, {"Ed"}
> });
> }
> }
>
> My output
>
> Before Class
> Before Method
> One
> After Method
> Before Method
> Two
> After Method
>
> Before Method
> Kartik
> After Method
> Before Method
> One
> After Method
> Before Method
> Two
> After Method
> Before Method
> Rakesh
> After Method
> Before Method
> One
> After Method
> Before Method
> Two
> After Method
> Before Method
> Ed
> After Method
> After Class
>
> It runs the unit tests three times. The testOne, testTwo outputs remain
> unchanged, the testThree output changes with the depending upon the name.
>
> Is this the expected behavior? I expected that testThree would run three
> times each with a different parameter. Does anyone know if there is a reason
> for this implementation.
>
>
>



--
Regards,
Puneet


[Non-text portions of this message have been removed]




 
Advanced
Add to My Yahoo!      XML What's This?

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help