Home » Tag Archives: Mockito

Tag Archives: Mockito

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 »

Java: Mocking a ResultSet using Mockito

This post shows how you can mock a java.sql.ResultSet using Mockito. It can be used to help unit test code which performs operations on ResultSets (such as a ResultSetExtractor) without relying on an external datasource. You can create a MockResultSet by providing a list of column names and a 2D array of data. For example: var rs = MockResultSet.create( new ...

Read More »

Spring with Maven Tutorial

spring-interview-questions-answers

1. Introduction In this post, we shall demonstrate how to use Maven dependencies for Spring for very specific use-cases. The latest versions of all the libraries we use can be found on the Maven Central. Understanding how Maven dependencies work and how they are managed, is important in a project for an effective build cycle and the clear concepts about ...

Read More »

Convenient mocking in Mockito with JUnit 5 – the official way

Starting with the version 2.17.0 Mockito provides the official (built-in) support for managing a mocking life cycle if JUnit 5 is used. Getting started To take advantage of the integration, the Mockito’s mockito-junit-jupiter dependency is required to be added next to the JUnit 5’s junit-platform-engine one (see below for details). After that, the new Mockito extension MockitoException for JUnit 5 ...

Read More »

Expected Exception Rule and Mocking Static Methods – JUnit

Today I was asked to consume a RESTful service so I started implementing it following Robert Cecil Martin’s rules for TDD and came across a new way (atleast for me) of testing the expected exception along with the error message so thought of sharing the way I implemented it as part of this post. To start with let’s write a ...

Read More »

Using Mockito without static imports with Java 8

How to simplify Mockito usage by removing static imports in Java 8 based projects. Rationale Mockito API is based on static methods aggregated (mostly) in the (BDD)Mockito class followed by extremely fluent, chained method calls. Mock creation, stubbing and call verification can be initiated with mock/spy/given/then/verify static methods: @Test public void shouldVerifyMethodExecution() { //given TacticalStation tsSpy = BDDMockito.spy(TacticalStation.class); BDDMockito.willDoNothing().given(tsSpy).fireTorpedo(2); //when ...

Read More »

Hamcrest matchers tutorial

This article is part of our Academy Course titled Testing with Mockito. In this course, you will dive into the magic of Mockito. You will learn about Mocks, Spies and Partial Mocks, and their corresponding Stubbing behaviour. You will also see the process of Verification with Test Doubles and Object Matchers. Finally, Test Driven Development (TDD) with Mockito is discussed ...

Read More »