Junit Tests


Sometime user want to run existing junit test cases using TestNG, in such cases without refactor of junit test cases, you can run junit test case using testng. All you need to do is put the JUnit jar file on the classpath, specify your JUnit test classes in the testng.classNames property and set the testng.junit property to true

Lets create a Java class

package testngexp;
import org.junit.Test;

publicclass junitwithtestng {
@Test
publicvoid TestMethod(){
System.out.println("This Method will be executed with junit annoatation using testng.xml");
}
}

Here @Test annotation is junit annotation


TestNG.xml

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"verbose="1">
<testname="Test1"junit="true">
<classes>
<classname="testngexp.junitwithtestng"/>
</classes>
</test><!-- Test -->
</suite><!-- Suite -->

Here you should specifyjunit=trueeither in<suite> level or <Test> level in Testng.xml file

Results

image

Few points about Junit 3 and Junit4

JUnit 3:
All methods starting with test* in your classes will be run.
If there is a method setUp() on your test class, it will be invoked before every test method.
If there is a method tearDown() on your test class, it will be invoked before after every test method.
If your test class contains a method suite(), all the tests returned by this method will be invoked.

JUnit 4:
TestNG will use the org.junit.runner.JUnitCore runner to run your tests

Comments