Cosmic Module
J
Qubits of DPK
March 14, 2026
Core Java
Layman Explanation
When you build a bridge, engineers test every bolt before assembling. JUnit is Java's testing framework — you write small automated tests that verify each method works correctly, so bugs are caught early before they reach production.
What is JUnit 5?
JUnit 5 is the standard testing framework for Java. It lets you write automated unit tests that verify your code behaves correctly.
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
Basic Test Structure
java
QUBITS OF DPK
Key Annotations
javascript
QUBITS OF DPK
Common Assertions
java
QUBITS OF DPK
Parameterized Tests
java
QUBITS OF DPK
Test Lifecycle
javascript
QUBITS OF DPK
Production Use Cases
java
QUBITS OF DPK
️ Traps
java
QUBITS OF DPK
30-Second Interview Answer
"JUnit 5 is Java's standard unit testing framework. Tests are annotated with @Test, lifecycle methods with @BeforeEach/@AfterEach for per-test setup/teardown, and @BeforeAll/@AfterAll for one-time setup. Assertions like assertEquals, assertThrows, assertAll verify expected behavior. Parameterized tests run the same test with multiple inputs. Key principles: test one thing per method, use descriptive @DisplayName, test both happy path and edge cases. TDD (Test Driven Development) writes tests before implementation."
Interview Questions & MAANG-Level Answers
Q1. What is JUnit 5 and why is testing important?
JUnit 5 is Java's standard unit testing framework consisting of JUnit Platform (launcher), JUnit Jupiter (new API), and JUnit Vintage (legacy support). Testing importance: (1) Catches bugs early — fix at development, not production. (2) Enables refactoring — change code confidently with test safety net. (3) Documents behavior — tests show how code is supposed to work. (4) CI/CD gates — tests run automatically on every commit, blocking broken code from merging. At MAANG, code without tests is not accepted in PRs.
Q2. What is the difference between @BeforeEach and @BeforeAll?
@BeforeEach: runs before EACH individual test method — use for per-test setup like creating a fresh object. @BeforeAll: runs ONCE before ALL test methods in the class — must be static (JUnit creates one instance per class). Use for expensive one-time setup like starting a test database or loading a large file:
java
QUBITS OF DPK
Mirror annotations: @AfterEach (cleanup per test), @AfterAll (one-time cleanup).
Q3. How do you test that a method throws an exception?
Use assertThrows() which takes the expected exception class and a lambda:
java
QUBITS OF DPK
The lambda must throw the exception — if it doesn't, the test fails. You can also assert on the exception's message or properties.
Q4. What is a parameterized test?
A parameterized test runs the same test method with multiple different inputs, avoiding duplicated test code:
java
QUBITS OF DPK
Sources: @ValueSource (single values), @CsvSource (multiple fields), @MethodSource (from a method), @EnumSource (enum values). Greatly reduces test code and ensures edge cases are covered systematically.
Q5. What is TDD (Test Driven Development)?
TDD is a development approach where you write tests BEFORE writing the implementation. Red-Green-Refactor cycle: (1) Red — write a failing test for the feature. (2) Green — write minimum code to make the test pass. (3) Refactor — improve code quality while keeping tests green. Benefits: forces you to think about the API before implementation, results in 100% test coverage by design, and leads to simpler, more testable code. At MAANG, TDD is encouraged, especially for core business logic.
Q6. What is the difference between unit tests and integration tests?
Unit tests: test a single class/method in isolation, mock all dependencies, very fast (milliseconds), no external systems. Example: test Calculator.add() in isolation. Integration tests: test multiple components working together, use real databases/HTTP clients/message queues, slower (seconds), reveal integration issues. Example: test PaymentController making real DB call. In CI/CD: unit tests run on every commit (fast), integration tests run less frequently (nightly or pre-merge). Rule of thumb: 70% unit, 20% integration, 10% end-to-end (testing pyramid).