JUnit and TestNG Tutorial
Learn JUnit and TestNG step by step: 29 lessons in 10 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting started
- Why Test Automatically: JUnit, TestNG and the Java Testing Landscape Why automated tests pay off, how the test pyramid works, where JUnit 6 and TestNG fit in the Java ecosystem, and how this track mixes real code with stand-ins.
- Your First JUnit Test Write and run your first JUnit 6 test: the @Test annotation, arrange-act-assert, project layout, naming, and how to read a green or red test report.
- Your First TestNG Test Write your first TestNG test: the @Test annotation, Assert argument order, testng.xml, and the key differences from JUnit, with a runnable JavaScript stand-in.
JUnit core
- JUnit Assertions in Depth Master JUnit 6 assertions: equality and delta, assertAll grouping, assertThrows, iterables and arrays, lazy failure messages, plus AssertJ and Hamcrest as alternatives.
- Lifecycle, Nested Tests and Display Names Understand the JUnit 6 test lifecycle: @BeforeEach, @AfterEach, @BeforeAll, @AfterAll, per-method instances, @Nested groups, @DisplayName, @Disabled and ordering.
- Conditions, Tags and Selecting Tests Run the right JUnit 6 tests in the right place: @EnabledOnOs, @EnabledIf, assumptions, @Tag filtering, @RepeatedTest, @Timeout and selecting tests with the Launcher.
JUnit advanced
- Parameterized and Dynamic Tests Run one JUnit test with many inputs using @ParameterizedTest, @CsvSource, @MethodSource and @EnumSource, then generate tests at runtime with @TestFactory.
- Writing JUnit Extensions Learn the JUnit Jupiter extension model. Write lifecycle callbacks, a ParameterResolver and a TestWatcher, use @RegisterExtension and @TempDir, and replace JUnit 4 rules.
- Testing Exceptions, Timeouts and Async Code Test exceptions with assertThrows, compare assertTimeout and assertTimeoutPreemptively, and test CompletableFuture code in JUnit without flaky Thread.sleep calls.
TestNG core
- TestNG Annotations and Lifecycle Learn TestNG's nine setup and teardown annotations, the order they run in, what happens when setup fails, and the enabled and description attributes of @Test.
- TestNG Assertions and Groups Learn TestNG's Assert and SoftAssert classes, why assertEquals argument order differs from JUnit, and how groups, include and exclude rules and meta-groups select tests.
- Dependencies, Priority and Test Ordering Control TestNG test order with dependsOnMethods, dependsOnGroups, alwaysRun, priority and invocationCount, and learn why dependent tests are usually a design smell.
TestNG advanced
- Data Providers and Factories Write data-driven tests with TestNG: @DataProvider, parallel providers, @Parameters, @Factory, and how they compare with JUnit parameterized tests.
- testng.xml, Suites and Test Configuration Configure TestNG runs with testng.xml: suite, test, classes, packages, groups, include/exclude, parameters, Maven integration and reports.
- Listeners, Retry and Parallel Execution Hook into TestNG runs with ITestListener and reporters, retry failures with IRetryAnalyzer, and run tests in parallel without sharing mutable state.
Comparing and migrating
- JUnit vs TestNG: Choosing Between Them Compare JUnit 6 and TestNG feature by feature: annotations, data-driven tests, groups, dependencies, parallelism, ecosystem and when each is the better choice.
- Migrating from JUnit 4 and Between Frameworks Move from JUnit 4 to Jupiter with the Vintage engine, map annotations and rules to extensions, convert between JUnit and TestNG, and migrate step by step.
Tooling and collaborators
- Mocks and Spies with Mockito Use Mockito with JUnit 6 and TestNG: @Mock, when/thenReturn, verify, ArgumentCaptor, spies and strict stubs, plus a runnable hand-written mock stand-in.
- Running Tests with Maven and Gradle Run JUnit and TestNG tests with Maven Surefire, Failsafe and the Gradle test task: filters, tags, parallel forks and how to read the XML reports.
- Coverage, Mutation Testing and CI Measure what your JUnit and TestNG tests execute with JaCoCo, check whether they notice bugs with PIT mutation testing, and gate builds in CI.
Quality and pitfalls
- Designing Good Tests Write JUnit tests that read like specifications: clear names, one behavior per test, builders, test doubles, no over-specification and readable failures.
- Flaky Tests and Common Mistakes Find and fix flaky JUnit and TestNG tests: shared state, order dependence, time, randomness, sleeps and singletons, with runnable before-and-after examples.
Real-world projects
- Project: Test-Driven Bank Account Build a bank account class with test-driven development: red, green, refactor with JUnit 6, exceptions, parameterized cases, edge cases and a transfer.
- Project: Testing a Shopping Cart with Mocks Test a shopping cart service that depends on a product catalog: hand-rolled fakes and spies, nested test groups, tags for slow tests and selecting runs.
- Project: Build a Mini Test Framework Build your own tiny JUnit-style test framework in Java with annotations, reflection discovery, lifecycle methods, assertions and a report, then see how real tools work.
- Project: Testing a REST API Suite with TestNG Design a TestNG suite for an HTTP API: data providers for many inputs, groups for smoke runs, parallel execution, listeners and reports, with a runnable Node stand-in.
Practice and review
- Practice: Exercises with Solutions Twelve graded JUnit and TestNG exercises with full solutions: assertions, exceptions, parameterized tests, nesting, dynamic tests, clocks, soft asserts and groups.
- JUnit and TestNG Interview Questions 40 JUnit and TestNG interview questions with short, accurate answers and small code examples: lifecycle, assertions, parameterized tests, groups, listeners and design.
- JUnit and TestNG Cheat Sheet A side-by-side JUnit 6 and TestNG cheat sheet: annotations, assertions, lifecycle order, parameterized tests, filtering, Maven and Gradle commands and common fixes.