Java JUnit Testing Cheat Sheet
Covers JUnit 5 test lifecycle annotations, assertions, parameterized tests, and Mockito mocking patterns for writing effective unit tests.
2 PagesIntermediateApr 10, 2026
Basic Test Structure
Core JUnit 5 lifecycle annotations.
java
import org.junit.jupiter.api.*;import static org.junit.jupiter.api.Assertions.*;class CalculatorTest { private Calculator calc; @BeforeEach void setUp() { calc = new Calculator(); // Runs before each test } @Test void addsTwoNumbers() { assertEquals(5, calc.add(2, 3)); } @AfterEach void tearDown() { calc = null; // Runs after each test }}
Common Assertions
Assertion methods from org.junit.jupiter.api.Assertions.
java
assertEquals(expected, actual);assertEquals(expected, actual, "custom failure message");assertNotEquals(a, b);assertTrue(condition);assertFalse(condition);assertNull(obj);assertNotNull(obj);assertThrows(IllegalArgumentException.class, () -> calc.divide(1, 0));assertAll( () -> assertEquals(4, calc.add(2, 2)), () -> assertEquals(0, calc.add(0, 0)));assertArrayEquals(new int[]{1, 2}, result);assertTimeout(Duration.ofMillis(100), () -> calc.slowOp());
Parameterized Tests
Run the same test logic with multiple inputs.
java
import org.junit.jupiter.params.ParameterizedTest;import org.junit.jupiter.params.provider.ValueSource;import org.junit.jupiter.params.provider.CsvSource;@ParameterizedTest@ValueSource(ints = {2, 4, 6, 8})void isEven(int number) { assertEquals(0, number % 2);}@ParameterizedTest@CsvSource({"1,2,3", "2,3,5", "10,20,30"})void addsPairs(int a, int b, int expected) { assertEquals(expected, calc.add(a, b));}
Key Annotations
Lifecycle and configuration annotations in JUnit 5.
- @Test- Marks a method as a test case.
- @BeforeEach / @AfterEach- Runs before/after every test method in the class.
- @BeforeAll / @AfterAll- Runs once before/after all tests in the class; method must be static (unless using @TestInstance(PER_CLASS)).
- @Disabled- Skips a test method or class, optionally with a reason string.
- @DisplayName- Sets a custom human-readable name for a test or class in reports.
- @Nested- Groups related tests in an inner non-static class for hierarchical organization.
- @Tag- Labels tests for filtered execution (e.g. mvn test -Dgroups=slow).
- @ExtendWith- Registers an extension, e.g. @ExtendWith(MockitoExtension.class).
Mocking with Mockito
Common Mockito patterns used alongside JUnit 5.
java
import static org.mockito.Mockito.*;@ExtendWith(MockitoExtension.class)class OrderServiceTest { @Mock private PaymentGateway gateway; @InjectMocks private OrderService service; @Test void chargesCustomer() { when(gateway.charge(100)).thenReturn(true); boolean result = service.checkout(100); assertTrue(result); verify(gateway, times(1)).charge(100); }}
Pro Tip
Prefer assertThrows over try/catch + fail() for exception testing - it returns the exception so you can also assert on its message, and it clearly signals the intended failure path.
Was this cheat sheet helpful?
Explore Topics
#JavaJUnitTesting#JavaJUnitTestingCheatSheet#Programming#Intermediate#BasicTestStructure#CommonAssertions#ParameterizedTests#KeyAnnotations#Testing#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance