ITestListener with Spring testNG test

794 views
Skip to first unread message

Padam Raj

unread,
Jul 14, 2017, 3:35:17 PM7/14/17
to testng-users
I have a class which looks like this:

public class A {

    public void print(){
        System.out.println("hello from A");
    }
}

I have created a bean out of this using an xml file (base-test.xml) using that as context in my testNG tests.

@ContextConfiguration(locations = "classpath:base-test.xml")
public abstract class BaseTest extends AbstractTestNGSpringContextTests implements InitializingBean{
    
    @Override
    public void afterPropertiesSet() throws Exception {
        // TODO Auto-generated method stub
        
    }
}

All my tests extends this base class where I am using ITestListener with following code

public class Testlistener implements ITestListener {

    @Autowired
    private A a;
    
    @Override
    public void onTestStart(ITestResult result) {
        a.print();    
    }

}


I get a nullpointer exception in test listener as "a" doesn't get autowired. However autowiring for "a" works fine in all the test classes.
What is the best way to fix this? Is there something like SpringJUnit4ClassRunner for testNG which can be used here?

Currently I am exposing the ApplicationContext in baseTest
 
    public ApplicationContext getApplicationContext(){
        return this.applicationContext;
    }

and explicitly wiring the testListener when onTestStart() is called:

BaseTest test = result.getInstance();
test.getApplicationContext().getAutowireCapableBeanFactory().autowireBean(this);

Krishnan Mahadevan

unread,
Jul 15, 2017, 4:36:13 AM7/15/17
to testng...@googlegroups.com

The Listener instantiation and lifecycle management is not done by Spring but its being managed by TestNG. That is perhaps why your dependency injection via the @Autowire doesn’t work and you see NPE.

So you can perhaps do something like this :

 

Define an interface within which you expose all end-user actions via methods. Have all your classes (the likes of your Class A) implement this interface and define the required behavior.

 

Now within your TestNG Listener (ITestListener implementation), you can do something like this :

 

public class Testlistener implements ITestListener {

 

    @Override

    public void onTestStart(ITestResult result) {

        Object instance = result.getMethod().getInstance();

        if (instance instanceof Operations) {

            ((Operations) instance).print();

        }

    }

}

 

interface Operations {

    void print();

}

 

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
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 https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages