Skip to main content
CourseFlix

Learn Java Unit Testing with Junit & Mockito in 30 Steps

4h 44m 35s
English
Paid

Learn how to write clear and solid unit tests with JUnit and Mockito. This page gives you a simple guide to what you will study in the course. You see the key skills you will build and how each step fits into real Java work.

What You Will Learn

You take 12 steps with JUnit and 18 steps with Mockito. Each step shows you a small idea. You use that idea in short and focused tests. By the end, you know how to test code with less setup and fewer mistakes.

Mockito Steps

Start With Mocks

  • Set up a project with JUnit and Mockito. Run your first green test.
  • See why mocks help you test code that calls other classes.
  • Learn what a stub is. Write a test with a stub and see its limits.
  • Write your first mock with Mockito. Mock a simple service.

Stubbing and Verification

  • Stub methods with return values, matchers, and errors.
  • Use simple BDD terms: Given, When, Then. Write tests with BDD style.
  • Check how many times a method runs on a mock.
  • Capture values passed into a mock using an argument captor.

Useful Mockito Tools

  • Use Hamcrest matchers to improve test checks.
  • Use annotations like @Mock, @InjectMocks, and @Captor.
  • Learn how MockitoJUnit.rule() works.
  • Apply Mockito in a small Spring example.

Advanced Mocking

  • Spy on real objects and see when to use a spy.
  • Learn why Mockito does not mock final or private methods.
  • Use PowerMock to mock static methods.
  • Call private methods with PowerMock.
  • Mock a constructor when needed.
  • Review what makes a good unit test.

JUnit Steps

Start With Basics

  • See why unit tests matter in everyday Java work.
  • Set up your first JUnit test.
  • Write a simple assertEquals test.
  • Refactor your first test to make it cleaner.

Core Test Skills

  • Use assertTrue and assertFalse.
  • Run setup and cleanup code with @Before and @After.
  • Run class‑level setup with @BeforeClass and @AfterClass.
  • Compare arrays in a test.

More JUnit Tools

  • Test expected errors.
  • Measure simple performance with JUnit tools.
  • Write parameterized tests.
  • Group tests into suites.

About the Author: udemy

udemy thumbnail

Udemy is a global online marketplace for teaching and learning. It connects students with instructors who share skills in many fields.

Who Uses Udemy

Millions of learners use Udemy to gain skills for work and personal growth. Companies, governments, and nonprofits also use Udemy to train their teams.

What Udemy Offers

Udemy provides a curated set of business and technical courses. These courses help teams build practical skills and support ongoing learning at work.

Watch Online 36 lessons

This is a demo lesson (10:00 remaining)

You can watch up to 10 minutes for free. Subscribe to unlock all 36 lessons in this course and access 10,000+ hours of premium content across all courses.

View Pricing
0:00
/
#1: Introduction
All Course Lessons (36)
#Lesson TitleDurationAccess
1
Introduction Demo
02:19
2
Three thing you need to know
01:46
3
JUnit Step 1 : Why is Unit Testing Important?
03:42
4
JUnit Step 2 : Setting up your first JUnit
08:06
5
Step 03 : First Successful JUnit. Green Bar and assertEquals
09:40
6
Step 04 : Refactoring Your First JUnit Test
07:45
7
Step 05 : Second JUnit Example assertTrue and assertFalse
14:46
8
Step 06 : @Before @After
07:03
9
Step 07 : @BeforeClass @AfterClass
03:31
10
Step 08 : Comparing Arrays in JUnit Tests
05:20
11
Step 09 : Testing Exceptions in JUnit Tests
03:34
12
Step 10 : Testing Performance in JUnit Tests
04:04
13
Step 11 : Parameterized Tests
11:12
14
Step 12 : Organize JUnits into Suites
03:25
15
An Overview
01:54
16
Mockito Step 01 : Setting up a Maven Project
07:32
17
Overview of this Section
02:11
18
Mockito Step 02 : Setting up SUT (System Under Test)
11:03
19
Step 03 : Stubbing Example - with Disadvantages of Stubbing
11:44
20
Step 04 : Your first Mockito code! Hurrah!!!
18:55
21
Basics of Mockito - Section Overview
01:06
22
Step 05 : Stubbing variations with Mockito - Argument Matchers & More...
13:05
23
Step 06 : BDD Style - Given When Then
13:29
24
Step 07 : Verify calls on Mocks
08:53
25
Step 08 : Capturing arguments passed to a Mock
08:44
26
Step 09 : Hamcrest Matchers
11:42
27
Step 10 : Mockito Annotations - @Mock, @InjectMocks, @RunWith, @Captor..
10:01
28
Step 11 : Mockito Junit Rule
03:41
29
Step 12 : Real world Mockito Example with Spring
16:09
30
Step 13 : Mockito Spy
10:04
31
Step 14 : Theory : Why does Mockito not allow stubbing final & private methods?
04:26
32
Step 15 : Setting up PowerMock and SystemUnderTest
08:38
33
Step 15 : Continued. Mocking Static Method
12:32
34
Step 16 : Invoking Private Methods
05:44
35
Step 17 : Mocking a Constructor
09:26
36
Step 18 : Writing Good Unit Tests
07:23
Unlock unlimited learning

Get instant access to all 35 lessons in this course, plus thousands of other premium courses. One subscription, unlimited knowledge.

Learn more about subscription