2
votes

I am trying to run multiple tests I've written in Java using testNG unit testing and selenium UI testing frameworks. I have a configuration file setup so to make it convenient to run multiple tests.

When I add in @BeforeTest and @AfterTest annotations to separate methods I create due to certain lines of common code to multiple tests, my tests all fail because the @BeforeTest failed to initialize. I get a java.lang.NullPointerException at almost all the locations in my code.

When I put in this setup and cleanup code into my multiple @Test methods, it works just fine.

Here is how I'm doing it

I have 2 java classes:

CaliforniaBenefitsTest.java 
TestConfig.java 

Both belonging to the same package:

com.company.qa.states.benefits

Here is my XML file:

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 


<suite thread-count="1" verbose="4" name="Config">

    <parameter name="configFile" value="config.properties" /> 
    <parameter name="testConfigFile" value="testConfig.properties" /> 
    <parameter name="proxy" value="notrequired" /> 


    <test name = "Tests1"> 

        <groups> 
            <run> 
                <include name="prerequisite" /> 
                <include name="runnable" /> 
            </run> 
        </groups> 
        <classes>               
                <class name = "com.company.qa.states.benefits.CaliforniaBenefitsTest"/> 
        </classes> 
    </test> 


    <test name="Tests2"> 
        <groups> 
            <run> 
                <include name="prerequisite" /> 
                <include name="runnable" />         
            </run>
        </groups>
        <classes>
                <class name = "com.company.qa.states.benefits.TestConfig"/> 
        </classes>
    </test>

</suite>

What am I doing wrong? How can I fix my XML file or @BeforeTest and @AfterTest methods. Please tell me how to fix this problem, driving me insane. If I didn't include certain information please ask me, and I will provide this information.

1
Please put your snippetNguyen Vu Hoang
My java code snippet? I don't understandmosawi
Yes, your java files' codeNguyen Vu Hoang
you will need to show us your java class files,for better understanding as to what are you trying to achieve.Mrunal Gosar

1 Answers

1
votes

Please refer to these steps-

  1. Run once your project

  2. Refresh your project

  3. Now, “test-output” folder will get created under your project – unfold the folder

  4. Double click the ‘index.html’ file; it will get open in the Eclipse Browser window

  5. Click on the ‘.xml’ link given on the left panel

  6. The test suite XML will get opened on the right-side panel

  7. Take a copy of the XML content

  8. Right click your project >> New >> File

  9. Give name of your test suite file (with.xml extension e.g. testng.xml) and click finish

  10. Paste the content that you copied from the ‘index.html’ and amend your suite file as per your needs

Reference-How to create 'testng.xml' using Eclipse