0

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>

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

someone told me extending classes in order to get their methods is not a good practice (I had a long chain of inheritance) and they recommended that I use dependency injection. when I started learning dependency injection, I was told to learn using Spring...
I cant test this now because I am having another problem elsewhere in the code, but as soon as I do test this I will let you know what happened
You're not using dependency injection here, but dependency lookup. You're loading a bean from a spring context. Using new WebDriverCustomMethods() would have the same effect, but would be simpler and faster.
Im confused about why should I use DI at all... it was so much easier when I had the chain of extended classes...
Extending a class must be done when you have a is-a relationship: a Banana is-a Fruit. Whan you have a use-a or has-a realationship, you use composition: Carpenter has-a hammer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.