ITestListener with Spring testNG test

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

ITestListener with Spring testNG test

Padam Raj
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);

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ITestListener with Spring testNG test

Krishnan Mahadevan

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/

 

From: <[hidden email]> on behalf of Padam Raj <[hidden email]>
Reply-To: <[hidden email]>
Date: Saturday, July 15, 2017 at 12:04 AM
To: testng-users <[hidden email]>
Subject: [testng-users] ITestListener with Spring testNG test

 

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);

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Loading...