Home » Mockito

Tag Archives: Mockito

Mockito: Why You Still Should Appreciate InjectMocks Annotation

Anyone who has used Mockito for mocking and stubbing Java classes, probably is familiar with the InjectMocks-annotation. I seemed a little harsh in an article a few years back about why you should not use @InjectMocks to auto-wire fields, even though I actually consider Mockito to be one of the most brilliant mocking frameworks for unit testing in Java. Every ...

Read More »

Mockito: Cannot instantiate @InjectMocks field: the type is an abstract class

Anyone who has used Mockito for mocking and stubbing Java classes, probably is familiar with the InjectMocks-annotation. Use this annotation on your class under test and Mockito will try to inject mocks either by constructor injection, setter injection, or property injection. This magic succeeds, it fails silently or a MockitoException is thrown. I’d like to explain what causes the “MockitoException: ...

Read More »

Mockito: Cannot instantiate @InjectMocks field: the type is an interface

Anyone who has used Mockito for mocking and stubbing Java classes, probably is familiar with the InjectMocks-annotation. Use this annotation on your class under test and Mockito will try to inject mocks either by constructor injection, setter injection, or property injection. This magic succeeds, it fails silently or a MockitoException is thrown. I’d like to explain what causes the “MockitoException: ...

Read More »

Mock Void method with Mockito

Hey guys! After our previous blog on difference between thenReturn and thenAnswer mockito methods, we are back with yet another interesting blog on Mockito. Here, we shall discuss “How to Mock Void method with Mockito”. Let’s get started! When writing code, there is always at least one method that returns ‘void’, and at some point in time we need to ...

Read More »

Mockito ‘thenReturn’ vs Mockito ‘thenAnswer’

When it comes to writing test cases for code, Mockito is one of the most common choices of developers. Then we face the question, how to use thenReturn() and thenAnswer() methods of Mockito? If you are facing this also, don’t worry every one faces or has faced this question and we are here to solve it!! So, get set go… ...

Read More »

Clean Code from the Trenches – Writing Executable Specifications with JUnit 5, Mockito, and AssertJ

Executable Specifications are tests that can also serve as design specifications. They enable technical and business teams to get on the same page by enabling the use of a common language (in DDD-world this is also known as Ubiquitous Language). They function as documentations for the future maintainers of the code. In this article we will see an opinionated way of ...

Read More »

Mockito Matchers Precedence

This post is opinion. Let’s look at the verify method in Mockito for testing in Java. Example: verify(myMock).someFunction(123) – expects that someFunction has been called on the mock ONCE with the input 123. These days I prefer the full BDDMockito alternative, so write then(myMock).should().someFunction(123). Same basic concept. The Three Matching Methods You can provide the value into the verifying function ...

Read More »

Seven Testing Sins and How To Avoid Them

Throughout this article I will be using Java within code snippets, whilst also using JUnit and Mockito. This article aims to provide examples of test code which can be: hard to readdifficult to maintain Following these examples the article will attempt to offer alternatives, which can be used to enhance the readability of your tests, which in turn will help ...

Read More »