How to Mock Variables for Interfaces and Casts in Java

One common issue when writing JUnit tests is how to create a mock when the variable is an an interface / implementation or a casting. For example, in the case of casting: The variable processor is casted to SomeOtherProcessor. That’s why when you create a mock with it, you have to cover both SomePaymentProcessor and… read more »

Difference between Mockito’s when/thenReturn and doReturn/when

A common dilemma while writing unit tests in Java is whether to use Mockito’s when/thenReturn or doReturn/when stubbings. Both when/thenReturn and doReturn/when are used in Mockito, a popular Java testing framework, to mock behavior of methods in an object. However, they are used in slightly different scenarios. when/thenReturn is used to mock the return value… read more »

How to Create a Simple Mockito Unit Test

Mockito is a popular Java framework which comes to great help in Unit testing. In Unit testing it’s important to get to a state where you can concentrate on testing a specific functionality or behaviour. Mockito can help you with this by mocking or simulating specific methods which you may need. It’s important to be… read more »