
Master unit testing for Java applications by starting from first principles and building toward writing your own tests, all designed for absolute beginners.
Join the private Java easily student support channels, the Facebook group, Udemy Q&A, and the website contact, to get timely help with Java unit testing using JUnit and Mockito.
Explore foundational theory and best practices for unit testing with JUnit and Mockito, learn project setup and structure, coding conventions, then cover mocks, ham quest, and code coverage.
Explore industry standard guidelines and best practices for writing unit tests that are easy to work with, maintain, interpret, and run, building a safety net for robust production code.
Test Java classes with unit tests to ensure they handle various inputs without exceptions and interact predictably, producing robust production code and reducing bugs.
Understand what JUnit is, a Java unit-testing framework with a defined API of classes, interfaces, and annotations, providing lifecycle callbacks and test runners with IDE and build-tool integrations.
Explore a simple unit test in JUnit 4 by testing a basic production class, using a test class with an @Test method and Assert-based assertions.
Learn the two main logical structures for unit tests—arrange-act-assert and given-when-then—and how to apply them to set up, invoke, and verify production code in a clear, consistent template.
Name the test class after the production class with a test suffix, declare it public, and place it in the same package under the test sources, keeping tests simple.
Learn how to write isolated unit tests using mock objects to minimize moving parts and keep external dependencies away. Ensure fast, repeatable test runs and predictable results across environments.
Explore how assertions express facts in unit tests, use preconditions and post conditions to evaluate code state, and determine test outcomes with JUnit and Hamcrest.
Set up a Java project with Maven or the command line, review the generated structure, and adopt coding conventions. Begin using the JUnit framework and API for testing.
Create a new maven project using the maven quickstart archetype to include jUnit and hamcrest dependencies, and enable auto import to reflect dependencies in the ide.
Review the quickstep archetype project structure, with source/main/java for production code and source/test/java for tests, and ensure test libraries use a dedicated test scope.
Learn basic conventions for creating unit tests in Java with JUnit: mirror the package, name tests after production classes with a test suffix, and ensure access within the same package.
Emphasize a hands-on approach to creating production code and testing it with unit tests, explaining features just in time through easy, in-context steps to master core concepts.
Create a basic unit test by adding a public void test method, apply the @Test annotation, and run the calculator add test to validate production code.
Run unit tests in the IDE to observe test success by viewing test classes and methods marked with @Test in the test runner, where green results indicate all tests pass.
explain how test failures show red when a runtime exception occurs, with a stack trace to locate the failure, and how to use JUnit's expected attribute to verify throwing exceptions.
Learn to fail tests explicitly using the Assert.fail method in unit testing, including supplying a message and observing the resulting assertion error during JUnit workflows.
Use assert.fail in JUnit tests to explicitly fail when validation errors do not occur, and handle exceptions in a catch block to perform extra assertions on the exception state.
Discover how the JUnit assertions API supports null checks, boolean conditions, and object equality with expected versus actual and array comparisons.
Structure unit tests with arrange-act-assert triple a pattern in Java, including setting up objects and mocks, performing the test action, and asserting outcomes.
Explore how assertion execution works in JUnit, using multiple assertions and fail-fast behavior to quickly identify and locate failing tests, with static imports for readability and debugging guidance.
Add descriptive messages to assertions to aid diagnostics in unit tests by using the message parameter in assertion methods, so failures print helpful descriptions.
Learn to code a unit test using arrange-act-assert in Java, writing test cases that compute 10 plus 20 and assert the result equals 30, with actual and static inputs.
Learn how the @Before annotation keeps unit test setup dry by centralizing the test fixture creation, reducing duplication across tests, and ensuring the setup runs before each test.
Understand how test fixtures create fresh objects for each test method using before and before class setup. Ensure each test runs against its own instance, with after and after class.
Advance your unit testing skills by exploring Hamcrest assertions, learning to use mock objects with Mockito, and measuring code coverage to meet quality gates.
Learn how mock objects from Mockito act as stand-ins for real collaborators in unit tests, letting you simulate authentication flows, control behavior, and verify interactions with your code.
Learn how the system under test interacts with collaborators and how mock objects simulate inputs and outputs to test different scenarios, including exceptions, while keeping tests simple and robust.
Configure mosquito as a test dependency in your maven or Gretel build files, ensure it's on the test classpath and isolated from production code.
Set up a new Java project for JUnit and Mockito, add the Mockito dependency, and create a test that demonstrates mocking a class with Mockito.mock.
Explore a layered architecture with presentation, service, and data persistence layers through a login controller that redirects to home or login based on authentication, testing with mock objects and Akita.
Explore a layered architecture by having the login controller delegate authentication to a separate authentication service, separating concerns and illustrating how login flow routes to home or login.
Explore coding the repository in the data layer, wiring an authentication service with a user repository, and prepare for unit testing by tidying the authentication and user validation flow.
Organize the application into web, service, and data packages to separate presentation, business logic, and persistence, showing login flow from controller to authentication service and user repository.
Demonstrates unit testing the login controller in a Java app using Mockito to mock the authentication service and constructor injection, verifying a successful login navigates to the home view.
Identify which objects belong in your test fixtures by examining the controller and authentication service relationships, and learn how to mock the user repository when testing the authentication service.
Write a test for the authentication service using a mock repository, configure it to throw an illegal argument exception, and observe the impact on test design and runtime behavior.
Iterate design decisions in unit testing to evolve robust code, handling exceptions with try-catch for propagated checked exceptions. Test different scenarios using an intuitive API.
Explore the Hamcrest assertions api with Mockito for mock objects, configure tests via annotations, and show how tests guide production code evolution in a continual testing journey.
Extend the domain and repository by enriching the user model with a boolean status, adding a regular/admin user type, and implementing static factories plus sample test users.
Learn how to temporarily ignore a failing test with the @ignore annotation, run all tests to detect regressions, and postpone fixes judiciously while maintaining the test suite integrity.
Flesh out the user repository and domain object to provide better test data, and implement find all to return all users from a fake database.
Implement business logic in the service by adding a method to fetch all regular users from the user repository as a set using streams.
Configure the maven compiler plugin in the pom to use java 8 by setting source and target to 1.8, ensuring portability and consistent builds between IDE and command line.
Evolve the repository class by using java streams to filter alive regular users and collect them into a set, and implement a second method for admin users.
Refactor service to a parameter-driven finder by using a private method with a user type enum, eliminating duplicates and enforcing the dry principle for unit testing with JUnit and Mockito.
Refactor the authentication logic by extracting it into a new user lookup service, reuse the repository, move methods, and run tests to ensure no regressions.
Learn to generate test methods for a lookup service in the IDE and toggle between production and test code, enabling quick setup checks, debugging, and defect detection.
Explore using the Mockito JUnit runner and the @Mock annotation to create mock user repositories for unit tests, with per-test mock instantiation and preparation for injection.
Analyze the production code to drive how you set up the arrange section of unit tests for Java developers using JUnit and Mockito, with thoughtful test data.
Wire a mock user repository into the user lookup service using @InjectMocks, then call get regular users on the system under test and prepare for unit or ham quest assertions.
Learn to validate the happy path in Java unit tests by running methods, using breakpoints, and mocking a repository to verify expected regular and admin users.
Use the JUnit assertions API with static imports to verify test results, asserting the users list size equals two and that each user's type, username, and password match expectations.
Examine weaknesses in the JUnit assertions API and discover how Hamcrest enables high-level, composable checks that reduce boilerplate, keep tests simple, and focus on testing behavior in Java unit tests.
Practice regular checks of your unit tests to spot mistakes early, backtrack when needed, and use assertions on collections like lists and sets to ensure correct behavior.
Upgrade your JUnit project to use hamcrest all by configuring the pom dependencies, replacing hamcrest core, and excluding transitive hamcrest from JUnit’s dependency.
Replace JUnit assertions with Hamcrest by asserting collections as a set of matches, order-insensitive, using contains in any order and static imports for readability.
Discover how Hamcrest produces readable error messages for test failures, showing how collections and objects are represented with square brackets and angled brackets, including examples like Donald and a password.
Reframe tests with hamcrest collection assertions to replace repetitive individual element checks, making tests more readable and concise by capturing the essence of what we're testing and eliminating copy-paste.
avoid invoking production methods in unit tests to preserve scientific quality and reduce bugs; when testing, compose expected properties instead of relying on the production class's object creation logic.
Understand how hamcrest matchers evaluate in JUnit tests by using assertThat with a collection and an any order matcher, and see how the equalTo matcher relies on Java equals.
Keep production code out of tests and limit object creation to the method under test, using alternatives to compare collection elements without constructors or factories.
Learn to compose hamcrest assertions using allOf and property matchers to test specific user properties (username and password) without relying on full object equals.
Do you want to get hands on demonstrations of JUnit and Mockito in action, want a simple no-fluff guide to how unit testing works including getting an understanding of best practices along the way?
Are you struggling to understand unit testing and would like a friendly guide to help you understand it all completely from the ground up?
Maybe you're a junior Java developer who is just starting out and you need a hands-on guide to JUnit and Mockito unit testing to get up and running quickly?
Or you might be an old hand at Java who has been out of practice and needs to level up your unit testing and JUnit and Mockito knowledge ready for a new project?
Then this course is for you!
We cover the following themes:
Foundations - this is all the good stuff that's been distilled down through the years - it's best practices and gets you up and running with knowing the stuff the APIs don't tell you
Installation and Set Up / Project Creation - we get you creating a project which uses JUnit and introduce you to the project structure and conventions you need to know
Writing Tests with JUnit - we cover exactly how to write unit tests with JUnit in this section and get a feel for the API together, covering best practices and how JUnit works along the way
Writing mock objects based unit tests with Mockito - we cover a quick start on getting up and running with Mockito to test a simple layered architecture you might find in a webapp (also coding the presentation, business and data layers in preparation to demonstrate this too!) then we demonstrate using Mockito to write tests for it
Using the Hamcrest assertions library - we cover how you can do more powerful assertions in your unit tests by harnessing the power of the Hamcrest assertions library that comes shipped with JUnit.
And the following topics:
Arrange/Act/Assert
Assertions
Testing Conventions
Good test characteristics
Java project structure
Creating a JUnit project with Maven
Running unit tests in the IDE
Test failures
JUnit's fail fast mechanism
JUnit's annotations
Test fixtures
Mock objects and their use in unit testing
Mockito basics
Layered architecture principles
Testing a simple layered architecture with Mockito and JUnit
Understanding the different types of assertions
Using the JUnit Assertions API and Hamcrest
Using Hamcrest matchers
Composing Hamcrest matchers and creating complex, yet readable assertions
Reading Hamcrest test failures
Analysing the production code to guide you to writing unit tests
and much, much more!
This course takes you straight from the beginning and gets you up and running to be fully proficient at writing unit tests using JUnit, Mockito and Hamcrest in quick time. Maybe you've started a new job where they use it to test their applications, or you're learning industry-standard tools to better place you ahead of the competition or you just want to ramp up your unit testing knowledge quickly in a no-fuss guide - then this course is for you!
Ultimately, by unlocking the mechanics of how JUnit and Mockito work in front of your eyes, you should be able to better test your Java projects with the frameworks and have fun doing so!
I look forward to teaching you all about JUnit, Mockito and unit testing inside! ;)