
Learn to test Java code with JUnit 5 and Mockito, practice test driven development, and configure Maven and Gradle projects using Intelligent Development Environment Community Edition or Eclipse.
Write small, self-contained unit tests that invoke a method and verify the expected result using arrange, act, and assert. Use JUnit to test valid and invalid inputs with mock dependencies.
Unit tests validate code with diverse inputs, prevent missed tests in large teams, and enable regression testing to keep features working after changes.
Explains the F.I.R.S.T principle for unit testing: fast, independent, repeatable, self validating, and timely, with practice on writing small, isolated tests that cover happy and negative paths.
Learn to test a method in isolation by using dependency injection to supply mocks or stubs and keep tests focused on the method under test.
Follow the testing pyramid: unit tests run fastest with no mocks, integration tests use real objects, and end-to-end UI tests validate the full flow.
Explore setting up JUnit across various project types using Maven and Gradle, with IDE support from Eclipse, NetBeans, Anthology, and VS Code, and create projects with or without build tools.
Create a new Maven-based project in IntelliJ IDEA, set the project name and location, and fill in group and artifactId coordinates to prepare for g unit dependencies.
Add junit-jupiter dependencies to a maven project by including junit-jupiter-api, junit-jupiter-engine, and junit-jupiter-params, or use the single junit-jupiter aggregator, then reload maven and run a demo test to verify.
Add the maven surefire plugin to the pom to run tests during mvn package or mvn test, with an option to skip tests.
Create a new gradle-based java project in an IDE, set artifact coordinates (group id, artifact id, version), and review the main and test directories and the build.gradle file.
Execute unit tests with JUnit 5 in a gradle-based project, create a demo test, and verify success via the integrated development environment and the terminal using gradlew clean test.
Explore how to add a new unit test in a basic Java project without Maven or Gradle, and see examples in Eclipse IDE alongside a discussion of different development environments.
Create a new Eclipse Java project, switch to the Java perspective, add a test folder and a JUnit test class, then run it to see passing or failing tests.
Create a first unit test for the calculator's integer division method. Use assert equals to verify that four divided by two returns two, illustrating a passing test.
Learn how assertions verify the method under test returns the correct result using assert equals and an optional message, with custom error messages appearing in test reports.
Explore JUnit assertions beyond assertEquals, including conditional fail and assertNotEquals, assertTrue, assertFalse, assertThrows, and assertDoesNotThrow, with static imports to drive pass/fail outcomes.
Practice a JUnit test by adding a subtraction method to a calculator, create a test for integer subtraction, and run the test or download an example test class for help.
Explore unit testing of a calculator's integer subtraction method using a test annotation, asserting 33 minus 1 equals 32, and cover valid and invalid inputs with multiple test methods.
Adopt a descriptive unit test naming convention using the template: test, system under test, underscore condition, underscore expected result, illustrated with integer division and division by zero.
Use the @DisplayName annotation to make test names readable and less technical. Apply display names above classes and methods to show friendly labels in the test report.
Learn to structure test methods using the arrange act assert pattern, also called triple a or given when then, to prepare inputs, invoke the method under test, and verify results.
Learn how JUnit test lifecycle methods manage setup and cleanup, including before all, before each, after each, and after all, and how test methods interact with lifecycle in unit tests.
Demonstrate JUnit 5 lifecycle annotations—before all, before each, after each, and after all—with setup and cleanup of resources, including a database, and per-test object initialization.
Learn to disable a unit test with a disabled annotation or by commenting out the test, and understand why you should avoid disabling tests to keep accurate, reported results.
Learn to verify arithmetic exceptions with JUnit 5 by using assertThrows, and practice arrange, act, and assert steps while testing integer division by zero.
Practice testing exceptions with JUnit by adding a square root method to a calculator and writing a test that asserts a legal argument exception for negative inputs.
Learn to write parameterized unit tests with @MethodSource, supplying multiple input arguments to a test method, and generate a stream of arguments to verify integer subtraction across cases.
Learn to use the @CsvSource annotation to supply multiple test arguments as comma-separated values, with curly braces and quotes for integers and strings.
Explore parameterized tests in junit 5 using csv file sources, store long parameter lists in a test resources folder, create and reference csv files, and run tests with multiple values.
Demonstrate parameterized tests with the value source annotation by supplying a string parameter, such as a first name, in repeated runs (John, Kate, Alice) and asserting it is not null.
Learn how to use the repeated test annotation in JUnit 5 to run a test method three times, access repetition information, and customize the display name for each iteration.
Configure a JUnit 5 test class to order test methods by name using a method order annotation, and verify tests run by name.
Learn to control test method execution order with the method order annotation and index, for integration tests involving database operations like save, find, update, and delete.
Discover how to order unit test classes in JUnit 5 with the order annotation, configuring a global test order via properties for user, product, and order service tests.
By default, JUnit creates a new test instance per method, ensuring isolation. Use the test instance annotation to share state within the same class when needed.
Demonstrate JUnit 5 test instance lifecycle, showing per-method vs per-class behavior with a calculator project and a string builder that reveals lifecycle effects.
Explore how a single test class instance, enabled by test instance lifecycle per class, runs ordered tests for create, update, get, and delete user.
Demonstrate test instance lifecycle in Java with four test methods and a before all setup, validating create, update, find, and delete on a temporary user database in one test class.
Explore test driven development in Java by writing unit tests first, then implementing minimal code to pass them, and iterating through red, green, refactor, and repeat.
Practice test driven development by creating a maven-based Java project, adding the jupyter dependency and surefire plugin, and implementing a user service test using arrange-act-assert to create a user.
Create a user service interface and its implementing class, organize them in a package, and ensure tests compile by instantiating the implementing class rather than the interface.
Test the user creation to ensure the returned user's first name matches the expected value, using arrange, act, and assert with a get first name method.
Refactor test methods in TDD by adding custom assertion messages, merging redundant tests, using a before-each lifecycle method, and adding a display name, then verify tests pass.
Practice test-driven development by extending a test to verify a user object's first name, last name, class name, and email address, while omitting the password for security.
Apply test driven development by extending the user object with last name and email, updating the constructor and getters, and validating changes with assertions.
Practice test driven development by writing a test for create user that throws an illegal argument exception when first name is empty, using assertThrows and before each to reduce duplication.
Apply test-driven development to validate create user, testing empty last name, email, password and repeated password; trigger illegal argument exception, implement validator with dependency injection, enforce password length and rules.
Learn how the Makita framework creates mock objects to test a create user method in isolation, replacing the data access object to avoid real database saves.
Learn to add the Mockito and JUnit dependencies to a Java project using Maven or Gradle, locate them in the Maven repository, and reload Maven to fetch the libraries.
Introduce the method under test that initially returns a user object with no persistence, and use Makita to create mocks while planning a user repository for storage.
Implement an in-memory user repository by defining a UserRepository interface with a save method and providing a simple hash map implementation, preparing for dependency injection into the user service.
Learn how to inject a user repository into a user service using constructor-based dependency injection, replace real dependencies with mocks for unit tests, and handle failures with a custom UserServiceException.
Learn to initialize mock objects with Mockito, annotate test classes, and inject mocks into the class under test using @ExtendWith, @Mock, and @InjectMocks.
Stub the save method on a mock user repository using Mikita's any() matcher. Return true or false as configured and optionally throw exceptions, keeping real code uninvoked for isolated testing.
Use Mockito to configure the user repository mock to throw when save is called, and verify the create user method results in a user service exception, demonstrating exception stubbing.
Create an email verification service interface and its implementation to queue email confirmations after user creation, then inject and mock this dependency to test the user creation method in isolation.
Use Mockito doThrow to stub a void method with an exception, mocking and injecting the email verification service, and verify the method is called once while throwing EmailNotificationServiceException.
Learn how to use Mockito do nothing to make void methods do nothing when invoked, overriding prior do throw setups for email verification services. See tests pass after restoring behavior.
Learn to make a mock call a real method with doCallRealMethod in Mockito, using an email verification service and email confirmation example, and verify the real method executes.
Learn to generate code coverage reports from unit tests and see classes, methods, and lines are covered, noting that 100% coverage isn't guaranteed and teams aim for 70 to 80%.
Run unit tests with coverage in IntelliJ to generate a code coverage report, explore package and class coverage, identify untested lines, and export the HTML report for team sharing.
Export and share code coverage reports from your unit tests in IntelliJ, generating HTML reports that show package, class, and line coverage with green and red highlights.
Configure a Maven base project to export regular unit test reports using the surefire report plugin during the test phase, and view the HTML report at target/site/surefire-report.html.
Learn how to configure the Jacoco Maven plugin to generate code coverage reports for a Maven project, including prepare agent and report executions during the test phase.
Learn how to unit test a Spring Boot get users endpoint in isolation by mocking the user service, avoiding the web server and Spring context, and distinguish from integration testing.
Perform end-to-end integration testing across web, service, and data layers in a Spring Boot application, using no mocks and the Springboard Test annotation to start a full application context.
Add testing support to a Spring Boot app by including Spring Boot starter web and test, plus security test, enabling unit and integration testing with JUnit 5 and Mockito.
Explore testing an existing springboard application with rest endpoints and spring security. Review the ui users controller, create user post /users, get users /users, and service layer with in-memory h2.
Create a new test class in the rest controller package, annotate with @WebMvcTest for the users controller, and exclude spring security filters to run focused web layer tests.
Build and perform an http post test for the create user endpoint with mock mvc, serialize user details to json, and validate the created user and id.
Use @MockBean to mock the user service in a web-layer integration test. Ensure the create user method integrates with the controller and Spring framework by returning a predefined user DTO.
Explore how the @MockBean annotation and Spring Boot validation work to validate a user creation request, trigger 400 bad requests for invalid emails, and verify the /users POST integration.
Validate the forward slash users api endpoint by adding a test that returns http 400 when the first name is missing, ensuring binding and size validations are enforced.
Review how a rest controller test verifies user creation returns 400 bad request when the first name is shorter than two characters, with before each setup and source code.
Unit Testing is a must-have skill and this video course is about unit testing.
If you take this video course, you will learn how to test your Java code using JUnit 5 and Mockito framework.
This video course is for beginners and you do not need to have any prior Unit testing knowledge to enrol in this course.
JUnit 5 in Different Java projects
There are different Java projects, build tools and development environments. In this course, you will learn how to create a new project and configure JUnit 5 support for different types of projects, using different development environments and build tools.
You will learn how to create a Unit test in:
IntelliJ IDEA and
Eclipse Development environments.
You will learn how to create a Unit test in:
Regular Java project,
Maven-based Java project,
Cradle-based Java project.
Course Overview
This video course teaches Unit and Integration testing with Java from the very beginning and covers many advanced topics as well. By the end of this course, you will learn:
JUnit 5 basics, and
JUnit 5 advanced topics.
Once you become familiar with JUnit 5, you will learn to use:
Test-Driven Development(TDD)
You will then learn to use another very popular Test framework for Java called:
Mockito
You will also learn how to write:
Spring Boot integration tests, and
Use Testcontainers to integrate real, containerized services(like databases) into your Java application tests, making sure your test scenarios are realistic without complex setup.
and how to test API endpoints using REST Assured.
By the end of this course you will learn and be able to use all of the following:
Create Unit Tests in IntelliJ,
Create Unit Tests in Eclipse,
Run unit tests using Gradle,
Run Unit Tests using Maven,
Use @DisplayName annotation,
Use JUnit assertions,
Test for Exceptions,
Use Lifecycle methods (@BeforeAll, @BeforeEach, @AfterEach, @AfterAll),
Run unit tests in any order you need: (Random, Order by Name, Order by Index),
Disable Unit test,
Repeated Tests with @RepeatedTest annotation,
Parameterized tests with @Parameterized annotation
@ValueSource,
@MethodSource,
@CsvSource,
@CsvFileSource
Change Test Instance lifecycle with @TestInstance (PER_CLASS, PER_METHOD)
Learn to Mock objects with Mockito's @Mock annotation,
Learn to user Mockito's argument matches,
Mockito method stubbing,
Mockito Exception stubbing,
Verify method call,
Call Real Method,
Do nothing when a method is called,
Write integration tests for Spring Boot applications,
and more...