Synchronizing a setup method running TestNG and Maven

631 views
Skip to first unread message

Jeff

unread,
Jan 29, 2015, 11:10:15 AM1/29/15
to testng...@googlegroups.com
I have a suite of tests (4 so far) that share a common base class.  One of the methods is a @BeforeClass setup() method.

This method has a synchronized block that should run ONLY once.  The structure is:

public class ServiceTestBase extends TestNgBase {

    public static volatile boolean serverInitialized = false;

         ...

    @BeforeClass
    public void setup() throws InitializationException {
        synchronized (ServiceTestBase.class) {
            if (ServiceTestBase.serverInitialized == false) {
                System.out.println("ProcessName:" + ManagementFactory.getRuntimeMXBean().getName());
                System.out.println("Seeding server configuration: instanceId = " + this);

                      ....  //Do stuff only once

                ServiceTestBase.serverInitialized = true;  //Set Flag to skip
               }
          }
     }
}

The mvn command is:

     mvn test -D test=ServiceTest* -P wp-dev

Based on the output below, all instances are running in the same process and *should* be using the same class loader (right?) and thus be synchronizing on the same Class object, yet the code runs 4 times, once for each of the 4 test classes that inherit from ServiceTestBase.  

ProcessName:55372@workstation1
Seeding server configuration: instanceId = ServiceTestEntitlements@354dc2e6
   .... Doing some stuff for the first time

ProcessName:55372@workstation1
Seeding server configuration: instanceId = ServiceTestDatabase@67409450
   .... Doing some stuff for the second time ... SHOULD NOT DO THIS

ProcessName:55372@workstation1
Seeding server configuration: instanceId = ServiceTestUpdate@5413e967
   .... Doing some stuff for the third time ... SHOULD NOT DO THIS

ProcessName:55372@workstation1
Seeding server configuration: instanceId = ServiceTestHealth@71b1c270
   .... Doing some stuff for the fourth time ... SHOULD NOT DO THIS

I can't see what I'm missing. I tried just creating a static lock object to synchronize on instead of the Class object itself with the same results.

Any insight is MUCH appreciated.

Cédric Beust ♔

unread,
Jan 29, 2015, 12:56:29 PM1/29/15
to testng...@googlegroups.com
Can you display a stack trace whenever your class is instantiated?

Also, can you try to run by specifying a testng.xml (make sure there is only one <test> tag)? I'm wondering if maven is not doing something weird here.

-- 
Cédric


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
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.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

Jeff

unread,
Jan 30, 2015, 5:35:59 PM1/30/15
to testng...@googlegroups.com
It's been one of those weeks where I can't explain why things happened, but now things are working.  

I in my effort to narrow down the problem, I refactored my code to call the synchronized block as part of @BeforeSuite which seemed to work fine.  When I then reverted it back as part of the @BeforeClass workflow, things are inexplicably working again.

Thanks for the help!
Reply all
Reply to author
Forward
0 new messages