Category Archives: JUnit

Mockito’s mock Methods

In this post, We will talk and learn about different overloaded mock() methods of org.mockito.Mockito class Mockito’s class org.mockito.Mockito provides below overloaded mock Methods: public static <T> T mock(Class<T> classToMock): It creates mock object of given class or interface. public static <T> T mock(Class<T> classToMock, String name) : In this method we can specify mock name.… Read More »

Mocking Exception Throwing using Mockito

In this post, We will learn How to throw an Exception using Mockito Let’s try to understand the above concept using a demo project pom.xml

  The Output of the above project:   You May Also Like: What is Mocking? Why Need for mocking? What… Read More »

Mocking Void Methods with Mockito

In this post, We will learn How to mock void methods with Mockito Usually most of the time Mockito’s org.mockito.stubbing.Stubber.when(T) method is really good enough to mock an object’s behavior but when we need to mock a void method then we can’t use when(T) method. Mockito Mock Void Method Mockito provides the following methods that can be… Read More »

Adding behavior to mocked object in Mockito

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… Read More »

@Captor Annotation in Mockito with Example

In this post, We will talk and learn about @Captor Annotation in Mockito with Example Mockito ArgumentCaptor and @Captor Annotation Usually, Mockito’s ArgumentCaptor is used to capture arguments for mocked methods. ArgumentCaptor is used with Mockito to verify() methods to get the arguments passed when any method is called. This way usually, we can use additional JUnit assertions for our… Read More »

Mockito – Verifying Method Calls

We can use org.mockito.Mockito.verify(T mock) method to ensure whether a mock() method was called with required arguments or not. In other words, we can say that Mockito.verify(T mock) is used to confirm that specific interactions took place. Let’s try to understand the above concept using a demo project pom.xml 

Read More »

@InjectMocks Annotation in Mockito with Example

In this post, We will learn about @InjectMocks Annotation in Mockito with Example  Mockito’s @InjectMocks annotation usually allows us to inject mocked dependencies in the annotated class mocked object. This is very useful when we have an external dependency in the class want to mock. We can specify the mock objects to be injected using @Mock annotation.… Read More »

@Mock and @Spy Mockito Annotations With Example

In this post, We will learn about @Mock and @Spy Mockito Annotations With Example? @ Mock Annotation The most Frequently used annotation in Mockito is @Mock Use @Mock annotation to create and inject mocked instances without having to call Mockito.mock(abc.class) manually. We may use org.mockito.Mockito class mock() method to create a mock object of a given class or interface. @Spy Annotation Use @Spyannotation to spy on… Read More »