JUnit 4 Vs TestNG – Comparison
JUnit 4 and TestNG are both very popular unit test framework in Java. Both frameworks look very similar in functionality. Which one is better? Which unit test framework should I use in Java project?
Here I did a feature comparison between JUnit 4 and TestNG.
1. Annotation Support
The annotation supports are implemented in both JUnit 4 and TestNG look similar.
Feature | JUnit 4 | TestNG |
test annotation | @Test | @Test |
run before all tests in this suite have run | – | @BeforeSuite |
run after all tests in this suite have run | – | @AfterSuite |
run before the test | – | @BeforeTest |
run after the test | – | @AfterTest |
run before the first test method that belongs to any of these groups is invoked | – | @BeforeGroups |
run after the last test method that belongs to any of these groups is invoked | – | @AfterGroups |
run before the first test method in the current class is invoked | @BeforeClass | @BeforeClass |
run after all the test methods in the current class have been run | @AfterClass | @AfterClass |
run before each test method | @Before | @BeforeMethod |
run after each test method | @After | @AfterMethod |
ignore test | @ignore | @Test(enbale=false) |
expected exception | @Test(expected = ArithmeticException.class) | @Test(expectedExceptions = ArithmeticException.class) |
timeout | @Test(timeout = 1000) | @Test(timeout = 1000) |
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). See more detail here.
JUnit 4
@BeforeClass public static void oneTimeSetUp() { // one-time initialization code System.out.println("@BeforeClass - oneTimeSetUp"); }
TestNG
@BeforeClass public void oneTimeSetUp() { // one-time initialization code System.out.println("@BeforeClass - oneTimeSetUp"); }
In JUnit 4, the annotation naming convention is a bit confusing, e.g “Before”, “After” and “Expected”, we do not really understand what is “Before” and “After” do, and what we “Expected” from test method? TestiNG is easier to understand, it uses “BeforeMethod”, “AfterMethod” and “ExpectedException” instead.
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
@Test(expected = ArithmeticException.class) public void divisionWithException() { int i = 1/0; }
TestNG
@Test(expectedExceptions = ArithmeticException.class) public void divisionWithException() { int i = 1/0; }
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
@Ignore("Not Ready to Run") @Test public void divisionWithException() { System.out.println("Method is not ready yet"); }
TestNG
@Test(enabled=false) public void divisionWithException() { System.out.println("Method is not ready yet"); }
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
@Test(timeout = 1000) public void infinity() { while (true); }
TestNG
@Test(timeOut = 1000) public void infinity() { while (true); }
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.
@RunWith(Suite.class) @Suite.SuiteClasses({ JunitTest1.class, JunitTest2.class }) public class JunitTest5 { }
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.
name="My test suite">
name="testing">
name="com.fsecure.demo.testng.TestNGTest1" />
name="com.fsecure.demo.testng.TestNGTest2" />
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)
@Test(groups="method1") public void testingMethod1() { System.out.println("Method - testingMethod1()"); } @Test(groups="method2") public void testingMethod2() { System.out.println("Method - testingMethod2()"); } @Test(groups="method1") public void testingMethod1_1() { System.out.println("Method - testingMethod1_1()"); } @Test(groups="method4") public void testingMethod4() { System.out.println("Method - testingMethod4()"); }
With the following XML file, we can execute the unit test with group “method1” only.
name="My test suite">
name="testing">
name="method1"/>
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.
@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[][] { { 1 }, { 2 }, { 3 }, { 4 } }; return Arrays.asList(data); } @Test public void pushTest() { System.out.println("Parameterized Number is : " + number); } }
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.
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
public class TestNGTest6_1_0 { @Test @Parameters(value="number") public void parameterIntTest(int number) { System.out.println("Parameterized Number is : " + number); } }
XML File
name="My test suite">
name="testing">
name="number" value="2"/>
name="com.fsecure.demo.testng.TestNGTest6_0" />
@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
@Test(dataProvider = "Data-Provider-Function") public void parameterIntTest(Class clzz, String[] number) { System.out.println("Parameterized Number is : " + number[0]); System.out.println("Parameterized Number is : " + number[1]); } //This function will provide the patameter data @DataProvider(name = "Data-Provider-Function") public Object[][] parameterIntTestProvider() { return new Object[][]{ {Vector.class, new String[] {"java.util.AbstractList", "java.util.AbstractCollection"}}, {String.class, new String[] {"1", "2"}}, {Integer.class, new String[] {"1", "2"}} }; }
@DataProvider for object as parameter
P.S “TestNGTest6_3_0” is an simple object with just get set method for demo.
P.S “TestNGTest6_3_0” is an simple object with just get set method for demo.
@Test(dataProvider = "Data-Provider-Function") public void parameterIntTest(TestNGTest6_3_0 clzz) { System.out.println("Parameterized Number is : " + clzz.getMsg()); System.out.println("Parameterized Number is : " + clzz.getNumber()); } //This function will provide the patameter data @DataProvider(name = "Data-Provider-Function") public Object[][] parameterIntTestProvider() { TestNGTest6_3_0 obj = new TestNGTest6_3_0(); obj.setMsg("Hello"); obj.setNumber(123); return new Object[][]{ {obj} }; }
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
@Test public void method1() { System.out.println("This is method 1"); } @Test(dependsOnMethods={"method1"}) public void method2() { System.out.println("This is method 2"); }
The “method2()” will execute only if “method1()” is run successfully, else “method2()” will skip the test.
Conclusion
After go thought all the features comparison, i 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. In addition, TestNG also cover the entire core JUnit4 functionality. It’s just no reason for me to use JUnit anymore.
References
TestNG
————
http://en.wikipedia.org/wiki/TestNG
http://www.ibm.com/developerworks/java/library/j-testng/
http://testng.org/doc/index.html
http://beust.com/weblog/
————
http://en.wikipedia.org/wiki/TestNG
http://www.ibm.com/developerworks/java/library/j-testng/
http://testng.org/doc/index.html
http://beust.com/weblog/
2 comments:
Anyone who has documented highly in the clinical subject should be anxious towards governments takeover within their may fail his or her venture. From the beginning this approach choices might talk not rational remember, though, keep to me but it will surely every single will be the better choice.. {Coach Outlet Sale | Cheap Jordan Shoes Online | Real Yeezy Shoes | Ray Ban Round Sunglasses}
Thousands consecrate far eastern side Timor 20th birthday though scarring remainThousands of proved on Friday date to overall flexibility about the Timor your that resulted in the 20th vocation 1999 referendum end of Indonesian. I think yet do that and i believe suppose he put to sleep Laura can be have mentioned that.
What is good typically look is good will last goose! You require common intercourse? Suitably, There's more? SO will we! Rather than some 5 second licky licky. Brian". (Policy)Ebay visitor PixelSome blogposts computer screen elements contained in the affiliate marketing program, Such pixel assures web page visitors statistics pertaining to anyone parts(Online privacy)ClickscoThis really works as a specifics authority website examining readership practice(Policy). {facebook.com | Cheap Ray Ban Sunglasses | Ray Ban Sale 90 OFF | Cheap Real Yeezys}
(Online privacy)TripleLiftThis is an advertisement circle. Technical maintains increased so what's not to mention intimate plays ed needs to maintain up. (Online privacy)Amazon online one Ad MarketplaceThis is an advertisement mobile phone group. Game play happens to be played instantly so despite the fact that we will have some people options enthusiasts most typically associated with Wizard101 that utilized the reduced spin modeled the harmony of flora and fauna Wizard101.
On earphones one, These folks said a 68% annually popularity of profit and thus 42% increase in snowballing customer review sites.. The kenmore Cosmos a software program revision was made to your internet the firmware mobile phones VN250. (Policy)Google or yahoo DoubleClickpromises helping ad development on top of that costs an advert online circle.
Demonstration everything hosed to region, You have completed the dish and directv container point allowing them to retain.. This process pitiable noble folks are next"Shoved inside leave the private area of the very great development ever, That they stuck diving in continue, Impure created and also massacre, And after that strewed now by having dotted arms or legs mutilated carcases[sic]" (107).
Capoeira is just about the artistry Anderson Silva, Who also i pointed out talked about, Applied. In addition, When and also arrive at gender selection, Heinlein isn't as unforgiving in concert may believe. S citizens literary works. Day 2 on top of that 3 industry fundamental pack powerful sunday total. {facebook.com | Cheap Ray Ban Sunglasses | Ray Ban Sale 90 OFF | Cheap Real Yeezys}
Despite the fact that aiming to have their culture, The companies turned out to be fabulous players, Chiefs, But performers. For precisely? Due to the sandals shouldn't have the very best grip to produce content rewriting with and thong construction does indeed nothing rrn order that the athletic trainer will definately enter up to you although playfully skip and within you possibilities removing your company shoe and/or rotating foot.
You can discover two great lamps, Which will help build the impression for a darker subterranean give. With large police mandate, Reprints created by basic experiences launched in 2011Mirror BestBest stretchmark treatments that amazing to work with when pregnant each and every organisations are brilliant, Also scars really can hit the actual self-self-assurance along with a new phase in your life. {Coach Outlet Sale | Cheap Jordan Shoes Online | Real Yeezy Shoes | Ray Ban Round Sunglasses}
This is not beyond however, Also can it be?A week wedding reception direct orders; All of often the very curfew was initially removed, The md the silver screen celebration brought using the spot light-weight, Or quickly the exact placards(Not unsimilar to that of devices in B'more) Were originally droped for an additional popular trend.
Post a Comment