Comparing TestNG and JUnit: Features, Annotations, and Usage in Java Unit Testing
This article explains unit testing concepts, outlines the differences between TestNG and JUnit—including test suite configuration, annotations, grouping, parameterization, dependency handling, exception and timeout testing—and provides code examples to help developers choose the most suitable Java testing framework.
Unit Testing
Testing is not a single activity but covers many scenarios and can be classified by test level such as integration, unit, and system testing.
Unit testing involves testing the smallest pieces of code—functions, modules, objects, or methods—during development to ensure each component works as expected, helping to catch defects early. Popular frameworks include JUnit, TestNG, NUnit, PHPUnit, JMockit, etc.
JUnit, introduced in 1997, is an open‑source Java unit‑testing framework that is part of the XUnit family and allows developers to write and run repeatable tests.
TestNG is a Java‑based unit‑testing framework with advanced features such as flexible test configuration, parameter support, data‑driven testing, annotations, and various integrations. It can execute unit, end‑to‑end, and integration tests and generates reports showing passed, failed, and skipped tests.
Understanding the differences between TestNG and JUnit helps select the most appropriate unit‑testing framework.
Differences Between TestNG and JUnit
Both are top‑tier Java automation frameworks with their own strengths and weaknesses. Below are the main differences:
Test Suite
Test suites group test cases for simultaneous execution. JUnit added suite support in JUnit 5, while TestNG has supported it from the start. The way each framework runs suites differs, as shown in the following code snippets.
In TestNG, test suites run from an XML file:
<suite name="TestSuite">
<test name="Demo">
<classes>
<class name="com.fsecure.demo.testng.TestNGTest1" />
<class name="com.fsecure.demo.testng.TestNGTest2" />
</classes>
</test>
</suite>In JUnit, suites are defined with annotations such as @RunWith and @Suite :
@RunWith(Suite.class)
@Suite.SuiteClasses({
JUnit1.class,
JUnit2.class
})
public class JunitTest5 {
// code
}TestNG offers more flexible suite configuration, allowing classes to be bundled into groups for execution.
Annotations
FEATURE
JUNIT 5
TESTNG
Mark method as test
@Test
@Test
Runs before first test method in class
@BeforeAll
@BeforeClass
Runs after all test methods in class
@AfterAll
@AfterClass
Runs before each test method
@BeforeEach
@BeforeMethod
Runs after each test method
@AfterEach
@AfterMethod
Runs before all tests in a suite
NA
@BeforeSuite
Runs after all tests in a suite
NA
@AfterSuite
Runs before a test
NA
@BeforeTest
Runs after a test
NA
@AfterTest
Runs before first method in any group
NA
@BeforeGroups
Runs after last method in any group
NA
@AfterGroups
Ignore test
@Disabled (JUnit 4: @ignore)
@Test(enabled=false)
Expected exception
@Test(expected=Arithmetic...)
@Test(expectedException=ArithmeticException.class)
Timeout
@Timeout
@Test(timeOut = 1000)
In JUnit 4, @BeforeClass and @AfterClass methods must be static, a restriction not present in TestNG.
Test Case Management
Managing test execution is easier with TestNG compared to JUnit.
Grouped Tests
Only TestNG supports grouping tests, allowing multiple groups of test classes to be run separately using the @Test(groups={"group1", "group2"}) annotation.
Groups can be identified under <test> or <suite> tags in the XML configuration.
Ignoring Tests
Both frameworks allow skipping tests. JUnit uses @Ignore , while TestNG uses @Test(enabled = false) .
@Ignore
public void method1() {
// code
} @Test(enabled = false)
public void TestWithException() {
// code
}Parameterized Tests
Parameterized (data‑driven) testing reduces code duplication. TestNG uses @Parameters with values defined in testng.xml , whereas JUnit uses @ParameterizedTest .
Dependent Tests
TestNG supports method dependencies via @DependsOnMethods ; JUnit does not provide this feature.
@Test(dependsOnMethods = {"Login"})
// codeException Testing
Both frameworks support expected exception testing, but the syntax differs. TestNG specifies expectedExceptions in the @Test annotation, while JUnit can use assertThrows or @Test(expected=...) .
@Test(expectedExceptions = ArithmeticException.class)
public void FunTester() {
int i = 10/0;
}Timeout Testing
Both frameworks allow setting a timeout for a test method.
@Test(timeout = 1000)
public void method1() { /* do nothing */ } @Test(timeOut = 1000)
public void testThisShouldFail() { /* do nothing */ }Conclusion
Many testing frameworks support automation, depending on the testing goal. TestNG and JUnit are the most trusted frameworks for automated unit testing. TestNG overcomes several limitations of JUnit, simplifying testers' work. With TestNG, you can perform unit, integration, and end‑to‑end tests, whereas JUnit primarily covers unit testing.
阅读原文,跳转我的仓库地址
FunTester
10k followers, 1k articles | completely useless
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.