
Master Java unit testing with JUnit 5, covering assertions, exceptions, nested and parameterized tests, and lifecycle annotations, using Java 21, Maven, and IntelliJ IDEA.
Understand the fundamentals of unit testing, including system under test, class under test, and method under test, and explore why fast, simple, self-checking tests using JUnit for Java.
Discover how JUnit, the Java unit testing framework, uses annotations and assertions with test runners to verify code and integrates with IDEs and build tools like Maven or Gradle.
Create a Maven project in IntelliJ IDEA, configure Java 21, add the JUnit Jupiter engine dependency to the pom.xml, and prepare to write your first test in the next lecture.
Explore core JUnit concepts like the system under test, the method under test, a test class and test method, plus assertions and test runners in IDEs and build tools.
Create a calculator class with an add method, write a JUnit 5 test using the add test annotation, and verify results with assertions.
Learn to use the @Test annotation from org.junit.jupiter.api in JUnit 5 to automatically run test methods, validate factorial logic, and employ the assert equals method.
Explore how JUnit 5's assertTrue method validates boolean conditions, including its overloads, boolean suppliers, and lazy evaluation with lambda expressions, demonstrated through a simple student list example.
Learn the assert false method, a static assertion opposite to assert true that verifies a condition is false and drives test outcomes, with multiple overloads and lazy boolean suppliers.
Understand how to use the static assert null method in JUnit's Assertions class to verify an actual object is null, with overloads for a message and a lazy supplier.
Master the JUnit 5 assertNotNull method and its three overloads to verify non-null objects in tests, including lazy messages with a supplier, via a getStudentById example.
Explains how to use JUnit 5's assertEquals to verify actual vs expected values, demonstrates overloaded variants for primitives and objects, custom messages, and lazy-evaluated failures.
Apply the JUnit assertNotEquals method and its overloads to confirm values differ, opposite of assertEquals, using id, name, supplier lambdas, and object comparisons in tests.
Explore JUnit 5's assertArrayEquals to verify that actual and expected arrays are equal in elements, order, and length, and learn overloads, custom messages, and supplier-based assertions.
Master JUnit's assertIterableEquals to verify two iterables are deeply equal, checking elements, order, and size across lists and other iterable types, via three overloaded variants.
Learn how to use JUnit 5's assertThrows to verify a code block throws a specific exception, including overloads with message and lazy message via a supplier.
Explore the JUnit assertThrowsExactly method, which asserts the exact exception type (no inheritance) and demonstrates its overloaded forms, lambda usage, and lazy evaluation with StudentNotFoundException and get student by name.
Learn how the @DisplayName annotation customizes test class and method names to improve readability of JUnit test results, including spaces, special characters, and emojis.
Learn how to disable tests with @Disabled in JUnit 5, for methods or classes, using optional messages, and when to disable due to known issues, incomplete features, or refactoring.
Learn how to use JUnit lifecycle annotations to manage test execution: before each, after each, before all, and after all, for setup and cleanup around Java unit tests.
Learn how the @BeforeEach annotation runs a setup method before every test method in a JUnit 5 class, reducing duplication and ensuring a clean test state with a calculator.
Use the after each annotation to run a teardown method after every test in JUnit 5. Learn how this cleanup closes resources such as files and database connections.
Use the @BeforeAll annotation to run a single setup before all tests; make it static to share resources such as a database connection or server startup for efficient unit testing.
Demonstrates using the @AfterAll annotation to run a static teardown method that cleans up shared resources, such as closing database connections or shutting down a server after all tests.
Learn how to use the @RepeatedTest annotation to execute a test method multiple times, customize display names, and apply it with before/after lifecycle annotations to verify consistency across repetitions.
Master nested tests in JUnit 5 by using @Nested to group non-static inner test classes and shared setup, organizing related tests within a clear hierarchical structure.
Learn to control the order of test method execution in JUnit 5 using test method order and add order annotations, with strategies like alphanumeric, random, and display name.
Group test cases into a JUnit 5 test suite and run them together with @Suite, @SelectClasses, and @SelectPackages, including IntelliJ examples and how to include multiple classes or packages.
Learn how to use JUnit 5 parameterized tests to run the same test method with different inputs, and explore value source, enum source, csv source, and method source.
Discover how to use value source with parameterized tests in JUnit 6, supplying int, string, and other literal values to run multiple test iterations with a single method.
Learn to use enum source to supply days enum values to a parameterized test, demonstrating is week day logic for weekdays and weekends.
Learn how csv source supplies comma separated values as test inputs for a factorial method in a MathTools class. Use @ParameterizedTest with @CsvSource in IntelliJ to verify results.
Learn how to supply parameterized test data from an external CSV file using JUnit 5 CSV file source and the @CsvFileSource sources attribute.
Use the method source type to supply test parameters from a static factory that returns a stream of arguments for a parameterized test, demonstrated with a factorial example.
Learn how to use parameterized tests with source type arguments by implementing an arguments provider and applying @ArgumentsSource to supply a stream of factorial test data.
This course is designed for beginners and experienced programmers to learn everything about the JUnit framework from scratch. All dependencies and examples in this tutorial are up-to-date and use the latest version of JUnit (JUnit 6+). In this course, you will learn unit testing for a banking app.
What is Unit Testing?
Unit testing is a way to check small parts of your program, like individual functions or methods, to make sure they work correctly. The main goals of unit testing are to:
Make sure each part of the program works as expected.
Make it easier to change and improve the code.
Ensure the program is reliable and bug-free.
By focusing on small parts, developers can find and fix bugs early, leading to better and more stable programs.
What is the JUnit 6 Framework?
JUnit is a framework that allows developers to create and run tests easily. It helps you write tests that automatically check if your code works correctly. JUnit is widely used because it makes testing simple and helps ensure your code behaves as expected.
JUnit Framework is a de-facto standard for writing unit tests in Java.
JUnit is a commonly used testing framework for Java projects. Knowing how to use JUnit is a valuable skill for any Java developer.
Writing tests with JUnit helps you find and fix bugs early in the development process, saving time and effort later.
What You Will Learn
Here are the key topics you’ll explore in this course:
Introduction to JUnit 6
Writing Your First JUnit Test
Steps to Write JUnit Tests
JUnit @Test Annotation
JUnit Lifecycle Annotations: JUnit @BeforeEach, @AfterEach, @BeforeAll and @AfterAll Annotations
JUnit Assertions
JUnit Exception Testing
JUnit Test Suites
JUnit Parameterized Tests
JUnit Nested Tests
JUnit Disable Tests
JUnit Display Test Names
JUnit Repeated Tests
JUnit Test Execution Order
Build Banking App
Write a JUnit test for Banking App
Tools and Technologies Used
Java 25
Maven
JUnit 6
IntelliJ IDEA