Search code examples
javaxmleclipsetestng

Generating TestNG.xml file from a simple "Hello World" code JAVA


I configured my eclipse environment to generate a TESTNG.xml file.

My code has this:

package testMy;

    public class testMy {
        public static void main(String[] args) {
            System.out.println("Hello!"); 
    
    }
        
    }

I installed TESTNG plugin and then i click on the project and selected option "convert to TESTNG"

enter image description here

I got this XML:

enter image description here

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Test">
  </test> <!-- Test -->
</suite> <!-- Suite -->

Is it valid? I do not see any "Hello!" message or something like that inside of that?

Am i missing anything?


Solution

  • Conclusion First

    testng.xml

    Add this under test

    <classes>
      <class name="myTest.DemoTest"/>
    </classes>
    

    Add unit test java DemoTest.java

    MyTest

    ├── pom.xml
    └── src
        ├── main
        │   ├── java
        │   │   └── testMy
        │   │       └── testMy.java
        │   └── resources
        └── test
            ├── java
            │   └── testMy
            │       └── DemoTest.java
            └── resources
                └── testng.xml
    

    testMy.java

    package testMy;
    
    public class testMy {
    
        public static void main(String[] args) {
             System.out.println("Hello!"); 
    
        }
        public String echo(String msg) {
            return msg;
        }
        public String hello(String msg) {
            return "Hello "+msg;
        }
    }
    

    testng.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <suite name="Suite" parallel="false">
      <test name="Test">
        <classes>
          <class name="myTest.DemoTest"/>
        </classes>
      </test> <!-- Test -->
    </suite> <!-- Suite -->
    

    DemoTest.java

    package testMy;
    
    import org.testng.Assert;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.Test;
    
    public class DemoTest {
        private testMy theOne=null;
        @BeforeClass
        public void setUp() {
            theOne =new testMy();
        }
        
    
    
          @Test
          public void testEcho() {
              Assert.assertEquals("AAAA",theOne.echo("AAAA"));
          }
          @Test
          public void testHello() {
              Assert.assertEquals("Hello AAAA",theOne.hello("AAAA"));
          }
    
    }