Adding behavior to mocked object in Mockito

By | October 15, 2020

Mockito adds functionality to a mock object using the when() method. Take a look at the following code snippet.

Here we are instructing the Mockito to provide or give a behavior of adding 10 and 40 to the add method of MyUtils and as a result, to return the value of 50

Let’s try to understand the above concept using a demo project

pom.xml

 

MyUtils.java

 

MyUtilsTest.java

 

The Output of the above project:

 

You May Also Like:

What is Mocking?
Why Need for mocking?
What are the Benefits of Mockito?
How to mock interface using Mockito example?
Mockito and JUnit Integration Using Maven Example
@Mock and @Spy Mockito Annotations With Example
@InjectMocks Annotation in Mockito with Example
Mockito – Verifying Method Calls
@Captor Annotation in Mockito with Example
Mocking Void Methods with Mockito
Mocking Exception Throwing using Mockito
Mockito’s mock Overloaded Methods

That’s all about Adding behavior to mocked object in Mockito
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *