[testng-users] BeforeSuite|Test|Class

146 views
Skip to first unread message

threecuptea

unread,
Oct 25, 2009, 1:36:17 AM10/25/09
to testng...@googlegroups.com

My project is written in EJB3. In order to test EJB, I have a parentClass
public abstract class EJBIntegrationTest with two BeforeSuites method
@BeforeSuite(alwaysRun = true)
@Parameters({"deploy_beans_xml", "scan_classpath","jndi_name_emf",
"jndi_name_usertx"})
public void startContainer(String deployBeansXml, String scanClasspath,
String jndiNameEMF, String jndiNameUserTx)
throws Exception {

EJB3StandaloneBootstrap.boot(null);
EJB3StandaloneBootstrap.deployXmlResource(deployBeansXml);
EJB3StandaloneBootstrap.scanClasspath(scanClasspath.replace("/",
File.separator));
}
//Initialize environment for all subclass.
@BeforeSuite(alwaysRun = true, dependsOnMethods="startContainer")
public void initialEnvironment() throws Exception {
jndi = new InitialContext();
programSession = lookupLocalBean(RemoteProgramSession.class,
"ProgramSessionEJB");
metaSession = lookupLocalBean(LocalMetadataSession.class,
"MetadataSessionEJB");
authoringSession = lookupLocalBean(LocalAuthoringSession.class,
"AuthoringSessionEJB");
dashboardSession = lookupLocalBean(LocalDashboardSession.class,
"DashboardSessionEJB");
EntityManager em = getEntityManagerFactory().createEntityManager();
session = (Session) em.getDelegate();
}

All EJB test classes extends EJBIntegrationTest with a BeforeClass method to
prepare default testing variables like the followings:
@Test(groups = "persistence-pandads-prgmgrp")
public class ProgramSessionTest extends EJBIntegrationTest {

@BeforeClass(groups="persistence-pandads-prgmgrp")
public void prepareSettings() {
dfltNetwork = programSession.getNetworkById(1);
dfltContentRating = programSession.getContentRatingById(5);
dfltProductType = programSession.getProductTypeById(2);
:
:}

It works fine until I add a second EJB test class MetadataSessionTest. I
found out prepareSettings method would always throws NullPointerException
for whichever test class run second (test run in random order) because it is
not able to access those EJBSession beans initialized in BeforeSuite method
initialEnvironment. prepareSettings method works fine for
whichever test class run first.

I tried many ways. Finally, I get it work by changing initialEnvironment.
to BeforeTest method. (Test includes all test classes). initialEnvironment
seems to run once for each test class (It's not my original intent)

I like to know
1) Why is the second test class unable to access variables initialized in
before test suite method? I like to know the difference of implementation of
BeforeSuite, BeforeTest, BeforeClass and how would that affect the running
sequence and accessibility.
2) What should we put in beforeSuite, beforeTest, beforeClass in principle?
What's your suggestion regarding to my case?

--
View this message in context: http://www.nabble.com/BeforeSuite%7CTest%7CClass-tp26045430p26045430.html
Sent from the testng-users mailing list archive at Nabble.com.

Cédric Beust ♔

unread,
Oct 25, 2009, 1:48:24 AM10/25/09
to testng...@googlegroups.com
A few thoughts.
  • Are you sure you want @BeforeTest and not @BeforeMethod?  The latter is much more common in my experience.

  • Can you simplify your code with just a few logs for each method called and post the log here?  This way we will see what methods are being invoked and in what order (you can also set verbose="10" in testng.xml and you'll get this information as well.

--
Cedric
--
Cédric


threecuptea

unread,
Oct 26, 2009, 2:21:37 PM10/26/09
to testng...@googlegroups.com

http://www.nabble.com/file/p26064775/BeforeSuite.log BeforeSuite.log
http://www.nabble.com/file/p26064775/BeforeTest.log BeforeTest.log
Hi Cedric:
I include the BeforeTest.log when I code initialEnvironment with that
annotation. In that case, all tests passed and
BeforeSuite.log when I code initialEnvironment with that annotation. In
that case, prepareSettings in ProgramSessionTest class throws
NullpointerException when access programSession and some tests failed or
skipped.
initialEnvironment tries to new InitialContext and lookup sessionBean and
EntityManagerFactory to be shared by child classes.
Also include excerpt of classes

public abstract class EJBIntegrationTest {
@BeforeSuite(alwaysRun = true)
public void startContainer() {
EJB3StandaloneBootstrap.boot(null);
EJB3StandaloneBootstrap.deployXmlResource(deployBeansXml);
EJB3StandaloneBootstrap.scanClasspath(scanClasspath.replace("/",
File.separator));
}

@BeforeSuite(alwaysRun = true, dependsOnMethods="startContainer")
public void initialEnvironment() {
jndi = new InitialContext();
programSession = lookupLocalBean(RemoteProgramSession.class,
"ProgramSessionEJB");
:
}
OR
@BeforeTest(alwaysRun = true)
public void initialEnvironment() {
jndi = new InitialContext();
programSession = lookupLocalBean(RemoteProgramSession.class,
"ProgramSessionEJB");
}
}

@Test(groups = "persistence-pandads-prgmgrp")
public class ProgramSessionTest extends EJBIntegrationTest {
@BeforeClass(groups={"persistence-pandads-prgmgrp"})
public void prepareSettings() throws Exception {
//line 59, NullPointerException if I use @TestSuite for
initialEnvironment method
dfltNetwork = programSession.getNetworkById(1);
}
}

@Test(groups = "persistence-pandads-metadata")
public class MetadataSessionTest extends EJBIntegrationTest {
@BeforeClass(groups={"persistence-pandads-metadata"})
public void prepareSettings() throws Exception {
dfltProductType = programSession.getProductTypeById(2);
}
}
<test name="Test1">
<classes>
<class name="com.ceg.panda.test.core.ejb.ProgramSessionTest"/>
<class name="com.ceg.panda.test.core.ejb.MetadataSessionTest"/>
</classes>
</test>


Cédric Beust ♔ wrote:
>
> A few thoughts.
>
> - Are you sure you want @BeforeTest and not @BeforeMethod? The latter
> is
> much more common in my experience.
>
> - Can you simplify your code with just a few logs for each method
View this message in context: http://www.nabble.com/BeforeSuite%7CTest%7CClass-tp26045430p26064775.html

Cédric Beust ♔

unread,
Oct 26, 2009, 2:48:46 PM10/26/09
to testng...@googlegroups.com
I'm sorry I don't understand what you are saying.

How about you
  • Post your code
  • Tell me what ordering of invocation you are seeing
  • Tell me what ordering you expected
?

--
Cédric

threecuptea

unread,
Oct 26, 2009, 3:38:05 PM10/26/09
to testng...@googlegroups.com

http://www.nabble.com/file/p26066107/EJBIntegrationTest.java
EJBIntegrationTest.java
http://www.nabble.com/file/p26066107/ProgramSessionTest.java
ProgramSessionTest.java
http://www.nabble.com/file/p26066107/MetadataSessionTest.java
MetadataSessionTest.java
Here you go.
Expected all passed but got NullPointerException in prepareSetting of either
ProgramSessionTest or MetadataSessionTest class depending upon which class
run second. See BeforeSuite.log in my previous email.

It works fine once I switch to use @BeforeTest annotation on
initialEnvironment. See BeforeTest.log in my previous email.


Cédric Beust ♔ wrote:
>
> I'm sorry I don't understand what you are saying.
>
> How about you
>
> - Post your code
> - Tell me what ordering of invocation you are seeing
> - Tell me what ordering you expected
View this message in context: http://www.nabble.com/BeforeSuite%7CTest%7CClass-tp26045430p26066107.html

Cédric Beust ♔

unread,
Oct 26, 2009, 4:59:08 PM10/26/09
to testng...@googlegroups.com
So it looks like initialEnvironment() is never called with @BeforeSuite but does get called if you use @BeforeTest instead?

Please set <suite verbose="10"> and post the log.

--
Cédric

threecuptea

unread,
Oct 26, 2009, 5:29:17 PM10/26/09
to testng...@googlegroups.com

I attached BeforeSuite.log & BeforeTest.log with my message posted at 11:21
AM. Let me know if you can access them.
Judging from BeforeSuite.log,
It did call once in initialEnvironment() with @BeforeSuite.
View this message in context: http://www.nabble.com/BeforeSuite%7CTest%7CClass-tp26045430p26067991.html

Cédric Beust ♔

unread,
Oct 26, 2009, 5:37:06 PM10/26/09
to testng...@googlegroups.com


On Mon, Oct 26, 2009 at 2:29 PM, threecuptea <sonya_l...@yahoo.com> wrote:
I attached BeforeSuite.log & BeforeTest.log  with my message posted at 11:21
AM.  Let me know if you can access them.
Judging from BeforeSuite.log,
It did call once in initialEnvironment() with @BeforeSuite.

Then I don't understand why you think this is an issue with TestNG(?).

--
Cédric


threecuptea

unread,
Oct 26, 2009, 6:12:13 PM10/26/09
to testng...@googlegroups.com

The issue is that only the first running test class can access those
sessionBean and EntityManageFactory retrieved in initialEnvironment() with
@BeforeSuite.
All the following running test classes is unable to access them. That's why
it throws NullPointerException.

I won't get the same trouble if I annotate initialEnvironment() with
@BeforeTest. I just like to know why.
Thanks.
--
View this message in context: http://www.nabble.com/BeforeSuite%7CTest%7CClass-tp26045430p26068686.html

Cédric Beust ♔

unread,
Oct 26, 2009, 6:21:57 PM10/26/09
to testng...@googlegroups.com
On Mon, Oct 26, 2009 at 3:12 PM, threecuptea <sonya_l...@yahoo.com> wrote:


The issue is that only the first running test class can access those
sessionBean and EntityManageFactory retrieved in initialEnvironment() with
@BeforeSuite.
All the following running test classes is unable to access them. That's why
it throws NullPointerException.

I won't get the same trouble if I annotate initialEnvironment() with
@BeforeTest.  I just like to know why.

The only explanation I can think of (which, again, does not seem to be TestNG related) is that you are setting these variables to null some time after the first test has run.  This would explain why your test fails with @BeforeSuite but not if you use @BeforeTest (and again, I think you really want to use @BeforeMethod and not @BeforeTest).

--
Cédric


Reply all
Reply to author
Forward
0 new messages