Author: Editorial Team
-
How does instanceof pattern matching enhance type safety in Java 16
Pattern matching for instanceof in Java 16 significantly enhances type safety and code reliability, as well as developer productivity, by changing how type checks and casts are handled in the language. What Changed in Java 16 Before Java 16, checking an object’s type and using it required two steps: Example (pre-Java 16): This approach, although common, is…
-
Difference between List.of() and Array.asList()
Here’s a clear comparison of List.of() and Arrays.asList() in Java: 1. Mutability 2. Null Handling 3. Backing 4. Use Cases 5. Example
-
How does Stream.peek and forEach differ in behavior
The methods Stream.peek() and Stream.forEach() in Java Streams are superficially similar—they both accept a Consumer and can execute code on each element—but they differ fundamentally in purpose, stream behavior, and best practices. Key Differences Aspect peek() forEach() Type Intermediate operation Terminal operation Stream Continuity Returns a new Stream (can chain more ops) Consumes the Stream (cannot reuse) Typical Intended Use Debugging,…
-
Saga Design Patterns: Choreography and Orchestration
Saga is a design pattern used to manage distributed transactions and ensure data consistency across microservices architectures. It decomposes long-running transactions into a series of smaller, manageable sub-transactions, each handled by a different service. There are two primary ways to implement the Saga pattern: Choreography (decentralized) and Orchestration (centralized). 1. What Is a Saga? 2. Saga Choreography 2.1 Overview…
-
JpaRepository vs CrudRepository
In Spring Data JPA, both CrudRepository and JpaRepository are interfaces used to access and manipulate data in the database. However, there are key differences in terms of functionality, inheritance hierarchy, and use cases. Below is a detailed comparison: 1. Inheritance Hierarchy 2. Package and Purpose Feature CrudRepository JpaRepository Package org.springframework.data.repository org.springframework.data.jpa.repository Purpose Basic CRUD operations…
-
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…
