Software Testing - JUnit Framework
1. What is JUnit?
JUnit is a popular unit testing framework for Java used to test individual units/components of code.
Developers and testers use JUnit to automate testing, validate logic, and ensure code quality.
JUnit is:
-
Lightweight
-
Easy to write tests
-
Supports annotations
-
Integrates with build tools (Maven, Gradle)
-
Works with CI/CD pipelines (Jenkins, GitHub Actions, etc.)
2. Why JUnit is Important
✔ Helps detect bugs early
Unit tests catch issues before integration or production.
✔ Supports automation
Tests run automatically with every build.
✔ Improves code quality
Encourages modular and maintainable code.
✔ Reduces debugging time
Clear failure messages identify exactly what broke.
✔ Widely used in Java projects
Industry-standard testing framework.
3. Key Features of JUnit
1. Annotations-Based Testing
JUnit provides annotations like @Test, @BeforeEach, etc.
2. Assertions
Used to check expected vs actual behavior
(e.g., assertEquals, assertTrue, assertNotNull).
3. Test Suites
Combine multiple test classes and run them together.
4. Parameterized Tests
Run the same test with multiple inputs.
5. Integrations
Works well with:
-
Maven
-
Gradle
-
Spring Boot
-
Selenium
4. JUnit Versions
The two major versions used today are:
✔ JUnit 4
Widely used, annotation-driven.
✔ JUnit 5
Latest version, modular, more powerful.
Also known as Jupiter Framework.
5. JUnit Common Annotations (JUnit 5)
| Annotation | Meaning |
|---|---|
| @Test | Marks a method as a test case |
| @BeforeEach | Runs before each test method (setup) |
| @AfterEach | Runs after each test method (cleanup) |
| @BeforeAll | Runs once before all tests |
| @AfterAll | Runs once after all tests |
| @Disabled | Skips the test |
| @ParameterizedTest | Runs test multiple times with different inputs |
| @DisplayName | Gives a readable test name |
6. JUnit Assertions
Assertions check whether expected = actual.
Common assertions:
assertEquals(expected, actual);
assertNotEquals(value1, value2);
assertTrue(condition);
assertFalse(condition);
assertNotNull(object);
assertThrows(Exception.class, () -> methodCall());
Assertions are the heart of unit testing.
7. Simple JUnit Test Case Example (JUnit 5)
Class to test:
public class Calculator {
public int add(int a, int b) {
return a + b;
}
}
JUnit Test Class:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class CalculatorTest {
@Test
void testAdd() {
Calculator calc = new Calculator();
assertEquals(5, calc.add(2, 3));
}
}
This test verifies that the add() method returns the correct result.
8. Using Lifecycle Methods
class ExampleTest {
@BeforeAll
static void setupAll() {
System.out.println("Runs once before all tests");
}
@BeforeEach
void setup() {
System.out.println("Runs before each test");
}
@Test
void test1() {
System.out.println("Test 1 executed");
}
@AfterEach
void cleanup() {
System.out.println("Runs after each test");
}
@AfterAll
static void cleanupAll() {
System.out.println("Runs once after all tests");
}
}
9. Parameterized Test Example
@ParameterizedTest
@ValueSource(strings = {"abc", "hello", "test"})
void testLength(String input) {
assertTrue(input.length() > 0);
}
Runs test multiple times with different strings.
10. Test Suites
A suite runs multiple test classes together.
JUnit 5 example:
@RunWith(JUnitPlatform.class)
@SelectPackages("com.example.tests")
public class TestSuite {
}
11. JUnit Integration with Selenium
JUnit is often used with Selenium for UI automation:
@Test
void testLogin() {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
assertEquals("Login Page", driver.getTitle());
driver.quit();
}
12. Best Practices
-
Write small and focused tests
-
Use meaningful test names
-
Follow Arrange–Act–Assert pattern
-
Add tests to CI/CD pipeline
-
Avoid test dependency
-
Mock external services (using Mockito)
-
Keep tests isolated