In this section, we will concentrate on Comparison between TestNG and JUnit with examples. Let’s discuss in details.
1. Annotation:
The annotation of TestNG is simpler than JUnit. The naming convention in case of JUnit is confusion. For example, the clarity of @Before , @After is bit confusion to understand that they mean actually.
JUnit:
@Before public static void oneTimeSetUp() { // one-time initialization code System.out.println("Before Method - oneTimeSetUp"); }
TestNG:
@BeforeMethod public void oneTimeSetUp() { // one-time initialization code System.out.println("Before Method - oneTimeSetUp"); }
2. Exception Handling:
TestNG and JUnit both support Exception handling feature. But in syntax wise these both are different. Let’s see the below example.
JUnit:
@Test(expected = ArithmeticException.class) public void checkException() { Code for exception handling }
TestNG:
@Test(expectedExceptions = ArithmeticException.class) public void checkException() { Code for exception handling }
3. Ignoring Test case:
Like Exception handling. Both are supporting the feature called Ignoring test which should ignore the unit test
JUnit:
@Ignore("Ignoring the Test") @Test public void checkIgnoringTestFeature() { System.out.println("Method is ignored"); }
TestNG:
@Test(enabled=false) public void checkIgnoringTestFeature () { System.out.println("Method is ignored"); }
4. Time Test:
This feature signifies that if a Test is talking longer time than expected then this test will be terminated and marked the Test as Failed state. Both TestNG and JUnit support this Time Test Feature. The time unit in this case is millisecond.
JUnit:
@Test(timeout = 100000) public void checkTime() { while (true); }
TestNG:
@Test(timeOut = 100000) public void checkTime() { while (true); }
5. Suite Test feature:
Suite test feature signifies running biding multiple test case in a single package and run them together. Both TestNG and JUnit support this features but in terms of method execution this is differ.
Let’s understand the difference with example.
JUnit:
If you want to run the suite test then you need 2 tag. One is “@RunWith” and another is “@Suite” are required to run the suite test. The below class means both unit test “Test1” and “Test2” run together after Test5 executed.
@RunWith(Suite.class) @Suite.SuiteClasses({ Test1.class, Test2.class }) public class Test5 { }
TestNG:
In TestNG, XML file is using to run the test. Let’s see below example for better understanding.
<suite name="My test suite"> <test name="testing"> <classes> <class name="Test1" /> <class name="Test2" /> </classes> </test> </suite>
With “Grouping” concept, you can tie every method with a group, it can categorize tests according to features. For example,
Here is a class with four methods, three groups (method1, method2 and method3)
@Test(groups="method1") public void testMethod1() { System.out.println("Method - testMethod1()"); } @Test(groups="method2") public void testMethod2() { System.out.println("Method - testMethod2()"); } @Test(groups="method1") public void testMethod1_1() { System.out.println("Method - testMethod1_1()"); } @Test(groups="method4") public void testMethod4() { System.out.println("Method - testMethod4()"); }
Following XML file will execute the unit test with group “method1” only.
<suite name="My test suite"> <test name="test1"> <groups> <run> <include name="method1"/> </run> </groups> <classes> <class name="TestNGTest5" /> </classes> </test> </suite>
5. Parameterized Test:
Parameterized Test define the declaring parameter in different way. TestNG and JUnit varies the way how they will implement using different method.
Let’s discuss below .
JUnit:
The “@RunWith” and “@Parameter” is use to provide parameter value for unit test, @Parameters have to return List[], and the parameter will pass into class constructor as argument.
@RunWith(value = Parameterized.class) public class JunitTest6 { private int number; public JunitTest6(int number) { this.number = number; } @Parameters public static Collection<Object[]> data() { Object[][] data = new Object[][] { { 5 }, { 256 }, { 543 }, { 24 } }; return Arrays.asList(data); } @Test public void pushTest() { System.out.println("Parameterized Number is: " + number); } }
It has many limitations here; we must follow the “JUnit” way to declare the parameter, and the parameter must pass into constructor in order to initialize the class member as parameter value for testing. Here “List []” is the return type of parameter class.
TestNG:
TO provide the parameter for testing, TestNG will use the XML file or @DataProvider.
XML file for parameterized test.
@Parameters declares in method that requires parameter for testing, the parametric data will be provided in TestNG XML configuration files. We can rerun the same test with different set of data by using this. In this way we can get the different result also with respect to data set.
Unit Test
public class TestNGTest6_1_0 { @Test @Parameters(value="number") public void parameterIntTest(int number) { System.out.println("Parameterized Number is: " + number); } }
XML File:
<suite name="My test suite"> <test name="testing"> <parameter name="number" value="2"/> <classes> <class name=" TestNGTest5" /> </classes> </test> </suite>
@DataProvider for parameterized test:
If you want to pull the data values into the XML file, it may be little complex to test occasionally. It may not represented as a String or Primitive value. TestNG handles this scenario with its @DataProvider annotation, which facilitates the mapping of complex parameter types to a test method.
Form the above section we have understood the comparison between TestNG and JUnit with example. In next section, we will discuss on Annotation.