Mockito - Stubbing with Fluent Interface

Mockito Stub - Stubying with Fluent Interface

Example 1

Sometimes we need to stub an object with different returns for the same method call. A real case is when you should mock iterations in the same mocked object.

This is not a common scenario, but if you must test it, Mockito will support you 🙂

In this example, we stubbied the method to return three values

  • Mockito
  • Guava
  • java 8

Those values will be returned when the method will be called once, twice and three times respectively

Example 2

This example will show us the same behavior of the previous one, but using consecutive calls

Note that this can be used as a Fluent Interface 🙂

Auxiliar UserDatabase class

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: