Category: junit
-
Adding a Data Layer and Unit Testing in Spring Boot with JPA, H2, and Mockito
Step 1: Add JPA and H2 Dependencies In your pom.xml, add the following dependencies for Spring Data JPA and the H2 in-memory database: Step 2: Configure Spring Boot Application Properties In src/main/resources/application.properties: Step 3: Create the JPA Entity Create Item.java in the appropriate package. This entity will map to the database table. Step 4: Create the Repository Interface Create…
-
Writing Unit Tests for Spring Boot Controllers with a Mocked Business Service
Overview In this tutorial, we will take the next step in our Spring Boot testing journey. Previously, we tested a simple controller method that returned a hardcoded JSON response. Now, we’ll introduce a business service into the controller, and learn how to write unit tests that mock the service, allowing us to test the controller…
-
Unit Testing JSON Responses with JsonAssert in Spring Boot
In this tutorial, we will explore how to write effective unit tests for REST controllers that return JSON responses. We will dive into using the JsonAssert library, which simplifies asserting JSON content by ignoring irrelevant differences such as spacing and order, and focusing only on the important fields. Prerequisites spring-boot-starter-test brings in dependencies like JUnit, Mockito, Hamcrest, and importantly, JsonAssert—which we’ll use…
-
Unit Testing RESTful Web Services in Spring Boot with MockMvc and Mockito
Testing is a crucial part of developing robust web applications, especially RESTful services built with Spring Boot. This detailed tutorial guides you step by step through the process of unit testing the web, business, and data layers of a Spring Boot application using MockMvc, Mockito, and key Spring testing features. You’ll also learn best practices for test…
-
Understanding Spies in Mockito
In this tutorial, we will learn about an important concept in Mockito testing called spying. Until now, you may have been working only with mocks. Spies behave differently from mocks in some critical ways. Let’s dive into it. 1. Introduction to Mocks When you create a mock of a class using Mockito, all the methods…
-
Mockito ArgumentCaptor for Multiple Calls
In this tutorial, we’ll explore a slightly more advanced use case in Mockito: capturing arguments when a method is called multiple times. This is especially useful when you want to validate that a method was called with different arguments on each call. 1. Problem Statement Suppose you have a mock object, and a method on…
-
Mockito – Verify method calls
In the previous steps, we used Mockito to mock or stub method behaviors. For instance, we told Mockito: These are called stubbing behaviors. However, stubbing alone is not sufficient for comprehensive unit testing. In real-world scenarios, we also need to verify interactions — i.e., whether certain methods were called and whether they were called with…
-
Using Generic Parameters with Argument Matchers in Mockito
Mockito provides powerful tools to stub method calls with specific or generic parameters. In this tutorial, we’ll focus on using generic parameters with argument matchers like anyInt() in unit tests. This helps simulate behavior when the method can be called with any value of a given type. 1. Objective To learn how to use argument…
-
Exploring Mockito with the List Interface
After exploring a real-life use case with Mockito, let’s now dive deeper into how the framework works by experimenting with one of Java’s core interfaces — List. In this tutorial, we’ll: Let’s get started. 1. Setting Up a Basic Mockito Test We’ll start by mocking a simple List object without using @RunWith or @ExtendWith. This…
-
Simplifying Unit Tests with @InjectMocks in Mockito
1. Objective We want to test a business class (SomeBusinessImpl) that depends on another component (SomeDataService). Traditionally, we would manually create a mock and inject it into the business class. However, Mockito offers a more elegant way to handle this using the @Mock and @InjectMocks annotations. 2. Problem Statement Suppose we have the following scenario:…
