Mockito - Mocking objects by using mock() and verify() methods

Mockito - Using Mocks with mock() and verify() methods

Example 1

In this example we will not use Mocked objects and instead of that we will use the Real object. Just to see the difference between Real and Mocked objects

Example 2

Now we're trying to retrieve a user which id does not exists

Example 3

Imagine that the UserDatabase makes a costly operation when try to find a user name given an id 🙁 Now it's time to mock our object!

One of the greatest feature to have when we'are using Mockito is its capability to record all interactions that a Mocked object had.

In this example we did a few things:

  • We mocked the UserDatabase class
  • We called the findNameById() method
  • We check if is true that the findNameById() method was called with the value 18

Example 4

In the following example the test will not pass since we are verifying that the method was called with a different id. Mockito just recorded that the method has called with id = 18

If you are new in the Mockito world, don't worry if you didn't understand yet 🙂 We will see a lot, A LOT of examples using Mockito!

That's it! I hope that this article can be really useful to you!

Thanks!

Alexandre Gama

Alexandre Gama

Hacking Code Founder

Hacking Code Founder and Writer, Passionate Senior Software Engineer and Team Leader at @Elo7, Teacher of several courses at Caelum, Speaker at many conferences and terrible guitar player.
Mockito Tutorial - Mocking Objects by using mock and verify - Java Guide by Hacking Code

Mockito - Mocking Objects by using mock() and verify()

Lear how to use Argument Matcher in its Fundamental way

Mockito Tutorial - Basic Usage of by when and thenReturn - Java Guide by Hacking Code

Mockito - Basic usage of when() and thenReturn() methods

Lear how to use Argument Matcher in its Fundamental way

Mockito Tutorial - Throwing Exceptions - Java Guide by Hacking Code

Mockito - Throwing Exceptions by using thenThrow()

Lear how to use Argument Matcher in its Fundamental way

Mockito Tutorial - Stubbing With Fluent Interface - Java Guide by Live Coding

Mockito - Stubying with Fluent Interface

Lear how to use Argument Matcher in its Fundamental way

Mockito Tutorial - Resetting Mock Interactions - Java Guide by Live Coding

Mockito - Reseting Mock Interactions

Lear how to use Argument Matcher in its Fundamental way

Mockito Tutorial - Ignoring Stubs - Java Guide by Hacking Code

Mockito - Ignoring Stubs Tutorial

Lear how to use Argument Matcher in its Fundamental way

Mockito Tutorial - BDD - Java Guide by Hacking Code

Mockito BDD - Behavior Driven Development with Mockito

Lear how to use Argument Matcher in its Fundamental way

Mockito Tutorial - Printing Custom Message - Java Tutorial by Hacking Code

Mockito - Printing Custom Message when the Verify fails

Lear how to use Argument Matcher in its Fundamental way

0 Comments

Leave a Reply

Share This
Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

%d bloggers like this: