Featured Article

Master Mockito Tutorial with JUnit 5: Complete Guide to Mocking

Kenneth Jul 13, 2026

Diving into unit testing with JUnit 5 and Mockito? You're on the right track! Let's explore how to leverage Mockito, an immensely popular mocking library in Java, to write cleaner and more efficient tests with JUnit 5. By the end of this tutorial, you'll be equipped to tackle mocking in your testing workflow like a pro.

crochet pattern for amigurma dolls with instructions to make them
crochet pattern for amigurma dolls with instructions to make them

Before we dive in, ensure you have the necessary tools: JUnit 5 (jado platform) in your project and Mockito in your testing scope. If you're using Maven, add these dependencies to your pom.xml:

an anime character with red hair looking at something on his cell phone, which is showing the time
an anime character with red hair looking at something on his cell phone, which is showing the time

{% raw %}

  org.junit.jupiter
  junit-jupiter-engine
   inscrites on the 5.4.2
  test


  org.mockito
  mockito-core
  inoscope of the 3.11.0
  test

{% endraw %}

Setting Up Mockito with JUnit 5

First, let's set up a simple JUnit 5 test using Mockito. We'll create a mock for a service layer and test its behavior.

the paper doll is showing how to make it look like she's wearing blue hair
the paper doll is showing how to make it look like she's wearing blue hair

Here's a basic example:

```java {% raw %} import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import static org.mockito.Mockito.when; import static org.assertj.core.api.Assertions.assertThat; @ExtendWith(MockitoExtension.class) class MyServiceTest { @Mock private MyService myService; @Test void testMethod_ShouldReturnMockedValue() { when(myService.someMethod()).thenReturn("mockedValue"); String result = myService.someMethod(); assertThat(result).isEqualTo("mockedValue"); } } {% endraw %} ```

Using Mockito Initializers

Junimo
Junimo

In the previous example, we used the @ExtendWith(MockitoExtension.class) annotation to enable Mockito initializers. This tells JUnit 5 to initialize and inject the mocks before each test. Let's dive into these initializers:

  • Interface initializers: These create mocks of interfaces. Use the @Mock annotation for this.
  • Class initializers: These create mocks of classes and abstract classes. Use the @Spy annotation for this.

Stubs and Mocks: When, Then, Verify

๐‘€๐‘–๐‘š๐‘–๐‘˜๐‘ฆ๐‘ข ๐‘ƒ๐‘Ž๐‘๐‘’๐‘Ÿ ๐‘๐‘Ÿ๐‘Ž๐‘“๐‘ก!! (๐‘š๐‘Ž๐‘‘๐‘’ ๐‘๐‘ฆ ๐‘š๐‘’)
๐‘€๐‘–๐‘š๐‘–๐‘˜๐‘ฆ๐‘ข ๐‘ƒ๐‘Ž๐‘๐‘’๐‘Ÿ ๐‘๐‘Ÿ๐‘Ž๐‘“๐‘ก!! (๐‘š๐‘Ž๐‘‘๐‘’ ๐‘๐‘ฆ ๐‘š๐‘’)

Mockito lets you set expectations and behaviors for your mocks using the when and then methods. After setting these up, use verify to ensure your mocks were used as expected:

```java {% raw %} when(mock.someMethod()).thenThrow(IOException.class); try { service_method_under_test(); } catch (IOException expected) { // Test passes if we reach here } verify(mock, times(1)).someMethod(); {% endraw %} ```

Mockito Verification Modes

Mockito offers various verification modes to check if your mocks were used as expected. Let's explore some of them:

Beaded infinity cubes tutorial part one: how to make a beaded cube
Beaded infinity cubes tutorial part one: how to make a beaded cube

Verification Times

Specify how many times a method should be called on a mock using the times argument:

Free DIY 4 x 4 Quilt Tutorial Video for Beginners | Jelly Roll Projects | Missouri Star Quilt Co.
Free DIY 4 x 4 Quilt Tutorial Video for Beginners | Jelly Roll Projects | Missouri Star Quilt Co.
three crocheted rabbits sitting next to each other on top of a white sheet
three crocheted rabbits sitting next to each other on top of a white sheet
ใ€็ฐกๅ˜ใƒ“ใƒผใ‚บใ‚นใƒ†ใƒƒใƒ/ใƒขใƒใƒผใƒ•ใ€‘ใƒšใƒจใƒผใƒ†ใ‚นใƒ†ใƒƒใƒใง็ทจใ‚€ๅฏๆ„›ใ„ใƒŸใƒ‹ใƒžใ‚ซใƒญใƒณใฎไฝœใ‚Šๆ–น No .24 Designed Oct 2020 Miistyle
ใ€็ฐกๅ˜ใƒ“ใƒผใ‚บใ‚นใƒ†ใƒƒใƒ/ใƒขใƒใƒผใƒ•ใ€‘ใƒšใƒจใƒผใƒ†ใ‚นใƒ†ใƒƒใƒใง็ทจใ‚€ๅฏๆ„›ใ„ใƒŸใƒ‹ใƒžใ‚ซใƒญใƒณใฎไฝœใ‚Šๆ–น No .24 Designed Oct 2020 Miistyle
the instructions for how to make an origami box with flowers and other items
the instructions for how to make an origami box with flowers and other items
an electronic device with numbers and symbols on it
an electronic device with numbers and symbols on it
Mochi blind bag๐Ÿกโ˜ #shorts #unboxing #papercraft #diy #mochi #blindbag
Mochi blind bag๐Ÿกโ˜ #shorts #unboxing #papercraft #diy #mochi #blindbag
an info sheet with instructions on how to use the font and numbers for this project
an info sheet with instructions on how to use the font and numbers for this project
the paper model of an old fashioned fire hydrant
the paper model of an old fashioned fire hydrant
Crochet Wiggly Tail Kitty | ๐Ÿฑ Easy Amigurumi Plush Charm Tutorial ๐Ÿพ Beginner Friendly
Crochet Wiggly Tail Kitty | ๐Ÿฑ Easy Amigurumi Plush Charm Tutorial ๐Ÿพ Beginner Friendly

```java {% raw %} verify(mock, times(1)).someMethod(); {% endraw %} ```

You can also use other verification modes like atLeast, atLeastOnce, and never.

Verification Order

Ensure your mocks are used in a specific order using the verification usinginOrder method:

```java {% raw %} verify(mock1).someMethod(); verify(mock2).anotherMethod(); verifyZeroInteractions(mock3); verifyInOrder(mock1, mock2); {% endraw %} ```

The above setup ensures mock1 is called before mock2, and there are no interactions with mock3.

Remember, Mockito helps you write better unit tests by isolating your code under test and focusing on its core logic. Happy testing!