TestListenerAdapter object creation and onFinish() invocation

657 views
Skip to first unread message

Dirk Hain

unread,
Oct 13, 2010, 2:59:31 PM10/13/10
to testng...@googlegroups.com
Hi everybody, 

I am running into an issue with a TestListenerAdapter that I wrote (extending TestListenerAdapter). The problem is that the framework seems to create 2 instances of my class. The result is that onFinish(..) is called twice which leads to repeated output. Here are my questions:

1. What triggers the TestListenerAdapter creation and how can there be mutliple instances?
2. When is onFinish(..) called by the framework. It appears to be after every suite though the javadoc says it is run after ALL test have run. 

I searched for more documentation but was not able to find details.

Thanks, 
-Dirk

Cédric Beust ♔

unread,
Oct 13, 2010, 3:08:05 PM10/13/10
to testng...@googlegroups.com
Hi Dirk,

I'm not sure I follow: you create the listener and you add it yourself to your TestNG object, TestNG doesn't create any.

I use TestListenerAdapter a lot to test TestNG itself, you can find some examples in the src/test/java directory.

Otherwise, can you share your code?

-- 
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.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.



--
Cédric


Dirk Hain

unread,
Oct 13, 2010, 3:35:38 PM10/13/10
to testng...@googlegroups.com
Hi Cédric, 

I created my TestListenerAdapter extension and use <listener> in my testng.xml files to attach it to my test suites. I am not using the TestNG object to register my listener. 
I am wondering what the lifecycle of the listener attached this way is. I have a multi module maven project that uses surefire to execute the tests. the listener registration looks like this:

<suite name="my-test" verbose="7">
  <test name="test1">
    <packages>
      <package name="some.package" />
    </packages>
  </test>
  <test name="test2">
    <packages>
      <package name="some.other.package" />
    </packages>
  </test>
  <listeners>
    <listener class-name="MyTestListenerExtension" />
  </listeners>
</suite>

Now when I run the tests 2 instances of my testlistener extension are created (did a little blackbox testing around this) and onFinish() is called on each instance. I am wondering why 2 instances are created and how to prevent it. I am assuming it is wrong configuration on my end but was not able to find more documentation besides the javadoc on the ITestListener interface.

Does this outline my situation a little better?

Thanks!
-Dirk


2010/10/13 Cédric Beust ♔ <ced...@beust.com>

Cédric Beust ♔

unread,
Oct 13, 2010, 3:54:44 PM10/13/10
to testng...@googlegroups.com
Hi Dirk,

I did a quick test on my end and things seem to work as expected (I'm using two <test> tags like you and each of them defines a <package>):

BListener created
[TestNG] Running:
  /Users/cbeust/java/testng/src/test/resources/package.xml

tmpa1
Success
tmpb1
Success

===============================================
Package
Total tests run: 2, Failures: 0, Skips: 0
===============================================

I get one listener instance created but onTestSuccess() gets called twice since there are two methods total.

Are you sure you have more than one instance of your listener created or are you just seeing two "onTestSuccess" calls? (which is normal)

-- 
Cédric

Dirk Hain

unread,
Oct 13, 2010, 4:32:49 PM10/13/10
to testng...@googlegroups.com
Hi Cédric, 

Thanks for checking so quickly. 
I am fairly sure that I have 2 instances since putting in  a static member (as a latch) to only run onFinish() once fixed the duplicate output vs. making it a class member still yielded the duplicate output. However, I think I found the real culprit which is that in the surefire configuration I had this entry:

<properties>
  <property>
    <name>listener</name>
    <value>MyTestListenerExtension</value>
  </property>
</properties>

I removed this entry et voila - no duplicate invocation. Looks like this configuration makes surefire create another instance of the listener. I am not sure if this is intended. 

Thanks again for your quick responses. You're the man!

Cédric Beust ♔

unread,
Oct 13, 2010, 4:52:13 PM10/13/10
to testng...@googlegroups.com
Dirk,

Indeed, you can specify listeners in Surefire as well, which explains the duplication you were seeing.

Glad to hear the mystery is solved.

-- 
Cédric
Reply all
Reply to author
Forward
0 new messages