Kaloyan Roussev
Kaloyan Roussev

Reputation: 14711

Spring Framework: how to pass a classes methods to another class' second method?

I have this class with a method in it (randomizer)

public class WebDriverCustomMethods {

        public int randomizer(int min, int max) {
            int d = (max - min) + 1;
            int c = min + (int) (Math.random() * ((d)));
            return c;
        }

that I want to use in my test class like so:

public class AppTest3 { 

    public static DataProviderClass appdata;
    public static WebDriverCustomMethods w;

    public static void main (String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        appdata = (DataProviderClass) context.getBean("data");  
        w = (WebDriverCustomMethods) context.getBean("wdcm");
    }

    @Test(dataProvider="standardTestData", dataProviderClass=DataProviderClass.class)
    public void oneUserTwoUser(WebDriver driver, WebDriverWait wait, ArrayList<ArrayList<String>> array) throws Exception {

            int randomNumber = w.randomizer(1, 99999);

when I run this and when the code gets to using w.randomizer(1,9999) I get the following error:

java.lang.NullPointerException
    at com.f1rstt.tests.AppTest3.oneUserTwoUser(AppTest3.java:34) //this is int randomNumber = w.randomizer(1, 99999);
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

This is my spring.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans SYSTEM "http://www.springframework.org/dtd/spring-beans-2.0.dtd" PUBLIC "-//SPRING//DTD BEAN 2.0//EN">
<beans>
    <bean id="data" class="com.blah.tests.DataProviderClass"/>
    <bean id="wdcm" class="com.blah.tests.WebDriverCustomMethods"/>
</beans>

Upvotes: 0

Views: 149

Answers (1)

JB Nizet
JB Nizet

Reputation: 691805

TestNG doesn't care about your main method. It will thus never be called when executing the tests, and the fields will thus never be executed.

Read the introduction of the documentation of TestNG, which explains the @BeforeXxx annotations.

That said, I don't know why you're using Spring in these tests, instead of just creating new instances of your 2 classes by yourself.

Upvotes: 2

Related Questions