JUnit_Documentation
JUnit_Documentation
Introduction to JUnit
JUnit is a popular open-source testing framework for Java that is primarily used for
unit testing. It allows developers to write repeatable and automated test cases to
ensure the correctness of their code. JUnit plays a crucial role in Test-Driven
Development (TDD) by enabling incremental code development through frequent
testing.
Project Structure
pom.xml Configuration
The pom.xml file manages dependencies and build configurations for the project.
This is a JUnit 4 dependency. It has been commented out, meaning it is not being
used in the current configuration. If enabled, it would provide JUnit 4 support for
running tests. The version is 4.12, and it is scoped to test, indicating it would only be
used during testing and not included in the final build.
JUnit 5 dependencies: These dependencies are for JUnit 5, which is the next
version of JUnit and is recommended for new projects. It consists of several modules,
each serving different purposes. Here, three dependencies are included:
o JUnit Jupiter API: This is the core API for writing tests in JUnit 5, which includes
annotations like @Test, @BeforeEach, @AfterEach, etc. The version 5.11.4 is
specified, and the scope is test to indicate it is used only in testing.
o JUnit Jupiter Engine: This dependency includes the engine required to actually
run the JUnit 5 tests. The junit-jupiter-engine runs tests written with the JUnit Jupiter
API. This is also scoped to test, meaning it's only used during the testing phase.
o JUnit Jupiter Parameters:
o This module provides support for parameterized tests, which allow you to run
the same test with different sets of input data. It is also scoped to test to
ensure it’s available only during testing.
o Summary:
The commented-out JUnit 4 dependency is unused.
The three active dependencies are for JUnit 5:
junit-jupiter-api: Used for writing tests.
junit-jupiter-engine: Required for running tests.
junit-jupiter-params: For parameterized tests.
2. CalculatorServiceTestJunit5.java
package com.myapp.Services;
import org.junit.jupiter.api.*;
import java.lang.annotation.Target;
@BeforeEach
public void beforeEach() {
System.out.println("Before Each Case");
}
@AfterEach
public void afterEach() {
System.out.println("After Each Case");
}
@Test
public void testAddTwoNumbers() {
int result = CalculatorService.addTwoNumbers(1, 2);
int expected = 3;
Assertions.assertEquals(expected, result, "Test failed!");
System.out.println("First test case");
}
// @Disabled
@Test
public void testAddAnyNumberTest() {
int result = CalculatorService.sumAnyNumbers(1, 2, 3, 4, 5);
int expected = 15;
Assertions.assertEquals(expected, result, "Test failed!");
System.out.println("Second test case");
}
// @Tag
// @Nested
// @TestFactory
}
The CalculatorServiceTestJunit5 class is a JUnit 5 test class that tests methods of the
CalculatorService class. Here's a brief explanation:
Annotations:
o @BeforeAll: Runs once before all test methods, typically for setup.
o @AfterAll: Runs once after all test methods, typically for cleanup.
o @BeforeEach: Runs before each test method, often used for preparing
the test environment.
o @AfterEach: Runs after each test method, typically for cleanup after
each test.
Test Methods:
The class uses JUnit 5 features like @Test, Assertions.assertEquals(), and lifecycle
annotations (@BeforeAll, @AfterAll, etc.) to structure and validate test cases.
Majority annotations used
Here’s an explanation of the major JUnit 5 annotations without examples:
1. @Test
2. @BeforeAll
Purpose: Runs once before all test methods in the test class. The method must
be static.
3. @AfterAll
Purpose: Runs once after all test methods in the test class. The method must
be static.
4. @BeforeEach
5. @AfterEach
6. @Disabled
7. @Tag
8. @Nested
9. @ParameterizedTest
10. @ValueSource
11. @TestFactory
12. @TestInstance
Purpose: Configures how instances of the test class are created, either
PER_METHOD or PER_CLASS.
13. @Timeout
Purpose: Specifies the maximum duration for a test to run. If the test takes
longer, it fails.