[testng-users] Order of execution of BeforeClass JVM dependent?

1,244 views
Skip to first unread message

Bob

unread,
May 7, 2010, 1:41:24 PM5/7/10
to testng-users
I've seen various threads about the order in which the BeforeClass
methods are run when in a class hierarchy (http://stackoverflow.com/
questions/2132734/beforeclass-and-inheritance-order-of-execution). For
example, a BaseTest has a method annotation with BeforeClass and a
subclass of the BaseTest has a method annotated with BeforeClass. Per
various discussions it's been stated that the superclass BeforeClass
would be executed prior to the subclass BeforeClass. I have not found
this to be the case, using 5.12.1 and Sun's 1.6.0_14 JVM (on windows).
In this setup (even using earlier versions of testNG) the order of
execution of the BeforeClass methods always seems to be child first,
then parent.

From some testing I did a while back I believe the order in which the
annotations were found and executed changed based upon which version
of the JVM I used. I looked into this a while ago so the details are
fuzzy (wish I would have posted back then), but I do recall that using
a different JVM caused a different execution order. I just can't
remember if it was a different version on the JVM on the same platform
or if it was a linux vs windows thing.

Before I start digging in further has anyone else experienced this? In
the meantime I'm using the 'dependsOnMethods' setting to get around
it, but would like to be able to remove it.

Thanks,
Bob

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

Cédric Beust ♔

unread,
May 7, 2010, 2:22:12 PM5/7/10
to testng...@googlegroups.com
Hi Bob,

Parent BeforeClass methods should always run before child BeforeClass methods, regardless of the JVM, let me know if you have a test case showing otherwise.

-- 
Cédric

--
Cédric

newbie

unread,
Jun 2, 2010, 6:18:20 PM6/2/10
to testng-users
Hi, Cedric,

I have two almost identical test cases. The first one consistently
runs the child's BeforeClass methods first. The second one
consistently runs the parent's BeforeClass methods first. And I am
using the same JVM.

I am getting around it by using the dependsOnMethods option.

Candy
> > testng-users...@googlegroups.com<testng-users%2Bunsubscribe@google groups.com>
> > .

Cédric Beust ♔

unread,
Jun 2, 2010, 7:30:23 PM6/2/10
to testng...@googlegroups.com
Can you email me that class?

Thanks.

--
Cédric

Gavin

unread,
Jun 15, 2010, 11:22:23 AM6/15/10
to testng-users
Cedric,

I'm running into this issue too. Did you ever resolve newbie's
issue? Did he send you his code?

Let me describe my environment a bit.

I'm using Spring 3.0.2 with TestNG 5.12.1 and JDK 1.6.0_11. I'm
running to the issue when I invoke the problematic test with Ant;
however, the issue doesn't present itself when I use the Eclipse
plugin.

I have a couple of nearly identical classes that are very simple, one
@BeforeClass, one @AfterClass and one or more @Test methods. Each
test extends AbstracTestNGSpringContextTests which has @BeforeClass as
well. This class does some dependency injection on variables that I'm
using in my test classes.

In one of the classes, my @BeforeClass method is called before the
parent class @BeforeClass method. In the other, the parent class
method is called before my method. I would expect the behavior to be
that the parent class method is called before my method.

Any thoughts? I'd be happy to send the code if you want to take a
look.
> > > > testng-users...@googlegroups.com<testng-users%2Bunsu...@googlegroups.com>
> > <testng-users%2Bunsubscribe@google groups.com>
> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/testng-users?hl=en.
>
> > > --
> > > Cédric
>
> > > --
> > > You received this message because you are subscribed to the Google Groups
> > "testng-users" group.
> > > To post to this group, send email to testng...@googlegroups.com.
> > > To unsubscribe from this group, send email to
> > testng-users...@googlegroups.com<testng-users%2Bunsu...@googlegroups.com>
> > .
> > > For more options, visit this group athttp://
> > groups.google.com/group/testng-users?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "testng-users" group.
> > To post to this group, send email to testng...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > testng-users...@googlegroups.com<testng-users%2Bunsu...@googlegroups.com>

Cédric Beust ♔

unread,
Jun 15, 2010, 1:57:06 PM6/15/10
to testng...@googlegroups.com
Hi Gavin,

As I explained before, in the absence of all constraints, TestNG will always run the before methods of superclasses first (and the after methods of superclasses last).  You can verify this yourself by writing a simple test with a base class.  I call this a "soft" (or "implicit") constraint.

Now, this ordering might be overridden if the user is specifying some constraints of their own, such as dependencies or using a method interceptor (I call this a "hard" or "explicit" dependency).

What would really help:  can you email me a test as simple as possible (ideally without any dependencies on Spring) that shows this behavior?  If there are no hard dependencies and the soft dependency is not being honored, then it's a bug.  On the other hand, if a hard dependency is overriding the soft dependency, then it's working as advertised.

Either way, I need a reproducible test case...

Thanks.

--
Cédric


To unsubscribe from this group, send email to testng-users...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.




--
Cédric

Matt Hauck

unread,
Dec 27, 2013, 12:47:16 PM12/27/13
to testng...@googlegroups.com
Okay, I was able to reproduce this as well. I think it has to do with the visibility of the before method itself. With a little bit of ad-hoc testing, it seems that the super method only gets run first as a BeforeMethod if it has protected visibility. Actually, looking a bit closer, it looks like the super before method doesn't even get run at all unless it has protected visibility!

(btw. I'm testing on TestNG 6.1.1 due to also having run into https://github.com/cbeust/testng/issues/438 -- https://groups.google.com/forum/#!topic/testng-users/tRJNxB-eihc --- I have yet to hear a reply about that one)

Here is a sample test class:

import java.util.*;
import static org.testng.Assert.*;
import org.testng.annotations.*;

class SuperSampleTest {

    List<String> order = new ArrayList<>(2);

    @BeforeMethod
    public void superSetup() {
        System.out.println("running super");
        order.add("super");
    }

}

public class SampleTest extends SuperSampleTest {

    @BeforeMethod
    public void childSetup() {
        System.out.println("running child");
        order.add("child");
    }

    @Test 
    public void verifyOrder() {
        if (order.get(0).equals("child"))
            fail("child before method ran before super!");
    }

}
Message has been deleted

Matt Hauck

unread,
Jan 30, 2014, 2:35:22 PM1/30/14
to testng...@googlegroups.com
I couldn't find any documentation on this either. It is not very intuitive at all. I think it is a bug: https://github.com/cbeust/testng/issues/473. Unfortunately, haven't seen any responses there from the maintainers either. =T

-- 
Matt Hauck

On Thursday, January 30, 2014 at 3:51 AM, Christian Mötzing wrote:

I can confirm your finding (by testing it myself). Is this a testng thing to only run annotated super methods with protected visibility? I can't find anything in the testng documentation.

--
You received this message because you are subscribed to a topic in the Google Groups "testng-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/testng-users/fJd90c1RPWM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to testng-users...@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.

吴亭

unread,
Jan 31, 2014, 4:17:39 AM1/31/14
to testng...@googlegroups.com
I try it in my side, I use the latest Inteij idea (inside attach the TestNG)

It works well for me whatever publlic or protected.

2013-12-27 Matt Hauck <matt...@gmail.com>:
> To unsubscribe from this group and stop receiving emails from it, send an
> email to testng-users...@googlegroups.com.
> To post to this group, send email to testng...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages