JUnit 5 Vs TestNG : Syntax Differences

Hashara Nethmin
3 min readJun 21, 2024

JUnit is a popular Java testing framework that provides annotations for test methods and assertions, supporting both unit testing and test-driven development practices. It has evolved with new features like parameterized tests and improved extension models.

In other hand TestNG, inspired by JUnit, offers flexible test configuration, parallel execution, and advanced annotations for test orchestration. It supports various testing scenarios, including unit tests, integration tests, and end-to-end tests, making it suitable for complex testing requirements.

Each with its own syntax and features. Here are some syntax differences between JUnit 5 and TestNG along with examples.

1. Annotations

Both syntax's are same but JUnit 5 utilizes @Test annotation from org.junit.jupiter.api package, while TestNG uses @Test annotation from org.testng.annotations package.

// JUnit 5
import org.junit.jupiter.api.Test;

public class JUnit5Example {

@Test
void testMethod() {
System.out.println("This is Annotations in Junit 5");
}
}

//---------------------------------------------------------

// TestNG
import org.testng.annotations.Test;

public class TestNGExample {

@Test
void testMethod() {
System.out.println("This is Annotations in TestNG");
}
}

2. Assertions

JUnit 5 assertions are provided by static methods in org.junit.jupiter.api.Assertions, while TestNG assertions are provided by methods in org.testng.Assert.

// JUnit 5
import org.junit.jupiter.api.Test;

public class JUnit5Example {

@Test
void testAssertions() {
String str1 = "JUnit";
String str2 = "JUnit";

assertEquals(str1, str2);
assertTrue(str1.equals(str2));
}
}

//---------------------------------------------------------

// TestNG
import org.testng.annotations.Test;

public class TestNGExample {
@Test
void testAssertions() {
String str1 = "TestNG";
String str2 = "TestNG";

Assert.assertEquals(str1, str2);
Assert.assertTrue(str1.equals(str2));
}
}

3. Dependency Injection

JUnit 5 supports dependency injection via constructor or method parameters using @BeforeEach, @AfterEach, @BeforeAll, @AfterAll, etc. annotations.

TestNG supports dependency injection through @BeforeMethod, @AfterMethod, @BeforeClass, @AfterClass, etc. annotations.

// JUnit 5
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class JUnit5DependencyInjectionExample {

private Calculator calculator;

@BeforeEach
void setUp() {
calculator = new Calculator();
}

@Test
void testAddition() {
assertEquals(4, calculator.add(2, 2));
}
}

//---------------------------------------------------------

// TestNG
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestNGDependencyInjectionExample {

private Calculator calculator;

@BeforeClass
void setUp() {
calculator = new Calculator();
}

@Test
void testAddition() {
Assert.assertEquals(calculator.add(2, 2), 4);
}
}

4. Grouping Tests

JUnit 5 doesn’t support native test groups, so tags or custom extensions are used. TestNG offers @Test(groups = “group1”) for grouping tests.

// TestNG
import org.testng.annotations.Test;

public class TestNGGroupingExample {

@Test(groups = "group1")
void testMethod1() {
System.out.println("This is testMethod1 in TestNG");
}

@Test(groups = "group2")
void testMethod2() {
System.out.println("This is testMethod2 in TestNG");
}
}

5. Parameterized Tests

JUnit 5 and TestNG both support parameterized tests, but their syntax differs.

// JUnit 5
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class JUnit5ParameterizedTest {

@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
void testWithValueSource(int argument) {
// logic with parameter
int expected = argument * 2;
int result = multiplyByTwo(argument);
assertEquals(expected, result);
}
private int multiplyByTwo(int num) {
return num * 2;
}
}
//---------------------------------------------------------

// TestNG
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;

public class TestNGParameterizedTest {

@Parameters({"myParameter"})
@Test
void testWithParameters(int myParameter) {
// logic with parameter
int expected = myParameter * 2;
int result = multiplyByTwo(myParameter);
assertEquals(expected, result);
}
private int multiplyByTwo(int num) {
return num * 2;
}
}

In conclusion, while JUnit 5 and TestNG share similarities in their basic functionality for testing in Java, they differ in syntax. JUnit 5 uses @Test for test annotations and provides assertions through Assertions class, whereas TestNG also uses @Test but relies on Assert class for assertions. Choosing between them often depends on project requirements and team preferences, but both are powerful tools for automated testing in Java applications.

--

--

Hashara Nethmin
Hashara Nethmin

Written by Hashara Nethmin

With a passion for tech writing. My interests lie at the intersection of technology and creativity,Through my writing.

No responses yet