JUnit is shipped with most of the IDEs such as Eclipse and NetBeans. There are many testing frameworks being developed using JUnit.
TestNG is an open source framework. It supports parametrization, data driven testing, parallel execution. JUnit is not much flexible for these.
I prefer TestNG over JUnit. I have developed open source testing framework using TestNG for Selenium. You may want to have a look at it.
Now either you may choose from any of the frameworks available like JUnit & TestNG or you can design our own framework.
Junit is a unit testing framework or the Java programming language. TestNG is specially designed to cover all types testing categories like Unit, Functional testing, Integration testing, End-to-end etc.
TestNG is a preferred framework of QA analysts as it allows you to generate test reports in both HTML and XML formats. TestNG enable you to group test cases easily which is not possible in JUnit.
- Annotation Support
The main annotation differences between JUnit4 and TestNG are
1. In JUnit 4, we have to declare “@BeforeClass” and “@AfterClass” method as static method. TestNG is more flexible in method declaration, it does not have this constraints.
2. 3 additional setUp/tearDown level: suite and group (@Before/AfterSuite, @Before/AfterTest, @Before/AfterGroup).
JUnit 4
1. @BeforeClass
2. public static void oneTimeSetUp() {
3. // one-time initialization code
4. System.out.println(“@BeforeClass – oneTimeSetUp”);
5. }
TestNG
1. @BeforeClass
2. public void oneTimeSetUp() {
3. // one-time initialization code
4. System.out.println(“@BeforeClass – oneTimeSetUp”);
5. }
2. Exception Test
The “exception testing” means what exception throws from the unit test, this feature is implemented in both JUnit 4 and TestNG.
JUnit 4
1. @Test(expected = ArithmeticException.class)
2. public void divisionWithException() {
3. int i = 1/0;
4. }
TestNG
1. @Test(expectedExceptions = ArithmeticException.class)
2. public void divisionWithException() {
3. int i = 1/0;
4. }
3. Ignore Test
The “Ignored” means whether it should ignore the unit test, this feature is implemented in both JUnit 4 and TestNG .
JUnit 4
1. @Ignore(“Not Ready to Run”)
2. @Test
3. public void divisionWithException() {
4. System.out.println(“Method is not ready yet”);
5. }
TestNG
1. @Test(enabled=false)
2. public void divisionWithException() {
3. System.out.println(“Method is not ready yet”);
4. }
4. Time Test
The “Time Test” means if an unit test takes longer than the specified number of milliseconds to run, the test will terminated and mark as fails, this feature is implemented in both JUnit 4 and TestNG .
JUnit 4
1. @Test(timeout = 1000)
2. public void infinity() {
3. while (true);
4. }
TestNG
1. @Test(timeOut = 1000)
2. public void infinity() {
3. while (true);
4. }
5. Suite Test
The “Suite Test” means bundle a few unit test and run it together. This feature is implemented in both JUnit 4 and TestNG. However both are using very different method to implement it.
JUnit 4
The “@RunWith” and “@Suite” are use to run the suite test. The below class means both unit test “JunitTest1” and “JunitTest2” run together after JunitTest5 executed. All the declaration is define inside the class.
1. @RunWith(Suite.class)
2. @Suite.SuiteClasses({
3. JunitTest1.class,
4. JunitTest2.class
5. })
6. public class JunitTest5 {
7. }
TestNG
XML file is use to run the suite test. The below XML file means both unit test “TestNGTest1” and “TestNGTest2” will run it together.
1. <!DOCTYPE suite SYSTEM “http://beust.com/testng/testng-1.0.dtd” >
2. <suite name=”My test suite”>
3. <test name=”testing”>
4. <classes>
5. <class name=”com.fsecure.demo.testng.TestNGTest1″ />
6. <class name=”com.fsecure.demo.testng.TestNGTest2″ />
7. </classes>
8. </test>
9. </suite>
TestNG can do more than bundle class testing, it can bundle method testing as well. With TestNG unique “Grouping” concept, every method is tie to a group, it can categorize tests according to features. For example,
Here is a class with four methods, three groups (method1, method2 and method3)
1. @Test(groups=”method1″)
2. public void testingMethod1() {
3. System.out.println(“Method – testingMethod1()”);
4. }
5.
6. @Test(groups=”method2″)
7. public void testingMethod2() {
8. System.out.println(“Method – testingMethod2()”);
9. }
10.
11.@Test(groups=”method1″)
12.public void testingMethod1_1() {
13. System.out.println(“Method – testingMethod1_1()”);
14.}
15.
16.@Test(groups=”method4″)
17.public void testingMethod4() {
18. System.out.println(“Method – testingMethod4()”);
19.}
With the following XML file, we can execute the unit test with group “method1” only.
1. <!DOCTYPE suite SYSTEM “http://beust.com/testng/testng-1.0.dtd” >
2. <suite name=”My test suite”>
3. <test name=”testing”>
4. <groups>
5. <run>
6. <include name=”method1″/>
7. </run>
8. </groups>
9. <classes>
10. <class name=”com.fsecure.demo.testng.TestNGTest5_2_0″ />
11. </classes>
12. </test>
13.</suite>
With “Grouping” test concept, the integration test possibility is unlimited. For example, we can only test the “DatabaseFuntion” group from all of the unit test classes.
6. Parameterized Test
The “Parameterized Test” means vary parameter value for unit test. This feature is implemented in both JUnit 4 and TestNG. However both are using very different method to implement it.
JUnit 4
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.
1. @RunWith(value = Parameterized.class)
2. public class JunitTest6 {
3.
4. private int number;
5.
6. public JunitTest6(int number) {
7. this.number = number;
8. }
9.
10. @Parameters
11. public static Collection<Object[]> data() {
12. Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } };
13. return Arrays.asList(data);
14. }
15.
16. @Test
17. public void pushTest() {
18. System.out.println(“Parameterized Number is : ” + number);
19. }
20.}
It has many limitations here; we have to follow the “JUnit” way to declare the parameter, and the parameter has to pass into constructor in order to initialize the class member as parameter value for testing. The return type of parameter class is “List []”, data has been limited to String or a primitive value for testing.
TestNG
XML file or “@DataProvider” is use to provide vary parameter for testing.
XML file for parameterized test.
Only “@Parameters” declares in method which needs parameter for testing, the parametric data will provide in TestNG’s XML configuration files. By doing this, we can reuse a single test case with different data sets and even get different results. In addition, even end user, QA or QE can provide their own data in XML file for testing.
Unit Test
1. public class TestNGTest6_1_0 {
2.
3. @Test
4. @Parameters(value=”number”)
5. public void parameterIntTest(int number) {
6. System.out.println(“Parameterized Number is : ” + number);
7. }
8.
9. }
XML File
1. <!DOCTYPE suite SYSTEM “http://beust.com/testng/testng-1.0.dtd” >
2. <suite name=”My test suite”>
3. <test name=”testing”>
4.
5. <parameter name=”number” value=”2″/>
6.
7. <classes>
8. <class name=”com.fsecure.demo.testng.TestNGTest6_0″ />
9. </classes>
10. </test>
11.</suite>
@DataProvider for parameterized test.
While pulling data values into an XML file can be quite handy, tests occasionally require complex types, which can’t be represented as a String or a primitive value. TestNG handles this scenario with its @DataProvider annotation, which facilitates the mapping of complex parameter types to a test method.
@DataProvider for Vector, String or Integer as parameter
1. @Test(dataProvider = “Data-Provider-Function”)
2. public void parameterIntTest(Class clzz, String[] number) {
3. System.out.println(“Parameterized Number is : ” + number[0]);
4. System.out.println(“Parameterized Number is : ” + number[1]);
5. }
6.
7. //This function will provide the patameter data
8. @DataProvider(name = “Data-Provider-Function”)
9. public Object[][] parameterIntTestProvider() {
10. return new Object[][]{
11. {Vector.class, new String[] {“java.util.AbstractList”,
12.”java.util.AbstractCollection”}},
13. {String.class, new String[] {“1”, “2”}},
14. {Integer.class, new String[] {“1”, “2”}}
15. };
16. }
@DataProvider for object as parameter
P.S “TestNGTest6_3_0” is an simple object with just get set method for demo.
1. @Test(dataProvider = “Data-Provider-Function”)
2. public void parameterIntTest(TestNGTest6_3_0 clzz) {
3. System.out.println(“Parameterized Number is : ” + clzz.getMsg());
4. System.out.println(“Parameterized Number is : ” + clzz.getNumber());
5. }
6.
7. //This function will provide the patameter data
8. @DataProvider(name = “Data-Provider-Function”)
9. public Object[][] parameterIntTestProvider() {
10.
11. TestNGTest6_3_0 obj = new TestNGTest6_3_0();
12. obj.setMsg(“Hello”);
13. obj.setNumber(123);
14.
15. return new Object[][]{
16. {obj}
17. };
18.}
TestNG’s parameterized test is very user friendly and flexible (either in XML file or inside the class). It can support many complex data type as parameter value and the possibility is unlimited. As example above, we even can pass in our own object (TestNGTest6_3_0) for parameterized test
7. Dependency Test
The “Parameterized Test” means methods are test base on dependency, which will execute before a desired method. If the dependent method fails, then all subsequent tests will be skipped, not marked as failed.
JUnit 4
JUnit framework is focus on test isolation; it did not support this feature at the moment.
TestNG
It use “dependOnMethods “ to implement the dependency testing as following
1. @Test
2. public void method1() {
3. System.out.println(“This is method 1”);
4. }
5.
6. @Test(dependsOnMethods={“method1”})
7. public void method2() {
8. System.out.println(“This is method 2”);
9. }
The “method2()” will execute only if “method1()” is run successfully, else “method2()” will skip the test.
Conclusion
After going thought all the features, i would suggest to use TestNG as core unit test framework for Java project, because TestNG is more advance in parameterize testing, dependency testing and suite testing (Grouping concept). TestNG is meant for high-level testing and complex integration test. Its flexibility is especially useful with large test suites.
Hope this Helps!!!