Now that you've learned about testng and its various tests, if you're worried about refactoring existing JUnit code, it's not necessary to use testng to provide a way to follow your own rhythm from JUnit and testng. You can also use TestNG to execute an existing JUnit test case.
TestNG can automatically identify and run JUnit tests, so you can use testng to run all the tests and write new tests using testng. All you have to do is put the JUnit library testng on the classpath, it can find and use the JUnit class, change the test run from JUnit and testng ant, and then run the testng "mixed" mode. This way you can test all the tests in the same project, even in the same package, and start using testng. This method can also convert your existing JUnit tests to testng.
Let's take a look at the following example and try out the above features:
To create a JUnit test case class
Create a Java class, which is a JUnit test class, Testjunit.java in C \ > Testng_workspace
Import Org.junit.Test;
Import static org.testng.AssertJUnit.assertEquals;
public class Testjunit {
@Test
public void Testadd () {
String str= "Junit testing using TestNG";
as Sertequals ("Junit Testing using TestNG", str);
}
}
Now, let's write testng.xml in C: \ > Testng_workspace should include the following <suite> tag as follows.
<?xml version= "1.0" Encodi Ng= "UTF-8"?
<! DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd",
<suite name= "converted JUnit Suite";
< Test Name= "Junittests" junit= "true";
<classes>
<class name= "Testjunit"/>
</classes>
</test>
</suite>
to perform the JUnit test Case definition Property junit= "True" as in the XML file above. The JUnit test case class testjunit is defined in the class name. In
JUnit 4, TestNG will run the test using Org.junit.runner.JUnitCore.
All Java class compilations use Javac.
C:\testng_workspace>javac Testjunit.java
now runs Testng.xml, which runs the JUnit test case for TestNG.
C:\TESTNG_WORKSPACE>JAVA-CP "C:\testng_workspace:c:\testng_workspace\lib\junit-4.11.jar" Org.testng.TestNG Testng.xml
Here I have placed the Junit-4.11.jar under the C:\TestNG_WORKSPACE\lib\junit-4.11.jar.
Verify the output.
===============================================
Converted JUnit Suite
Total tests Run:1, failures:0, skips:0
===============================================
TestNG Running JUnit Tests