JUnit: Misuse Of any() Matchers

For writing unit tests in JUnit, Mockito provides argument matchers that you can use to mock the arguments in a given() or when() statement. Among the matchers provided, the any() matcher lets you pass any argument to the method, ensuring that the test cases passes with any input. When any() matcher is used extensively, out of laziness of specifying the parameters, there are high chances that the test case is not really testing anything or it passes even with a wrong input....

July 23, 2021 · 2 min · Me

JPA: Persistence Context And Dirty Check Mechanism

Few entities were being persisted to database in certain cases, without explicitly calling save() or without having @Transactional annotation. Digging a little deeper, I realised this behaviour is due to persistence context and dirty check mechanism. Persistence Context Observation 1: Two entities, user and account, are retrieved from database and fields of both entities are modified. When save() was called only on account entity, both the entities were being persisted to database....

June 21, 2021 · 2 min · Me

Internals Of Java Parallel Streams

The Stream API was introduced in Java 8 as an efficient way to operate on collections. Parallel streams were introduced as a part of it for parallel processing and to make the application run faster. Though parallel streams are supposed to increase your application performance by splitting the task between multiple threads and completing them faster than sequential execution, there are chances parallel streams can sometimes slow down the entire application....

June 3, 2021 · 3 min · Me

Code Review as a Team Discipline

The widely accepted norm for reviewing code is to raise a Pull Request, open for comments from the entire team. However, this does not bode well when people prioritize their own work over reviewing. Either the Pull Request awaits reviews for a long time, or is even merged without a proper review. The unending delay to review causes the developers, the overhead of context switching between features while the merge conflicts start to pile up....

April 23, 2021 · 2 min · Me

JPA EntityGraphs: A Solution to N+1 Query Problem

The N+1 query problem is said to occur when an ORM, like hibernate, executes 1 query to retrieve the parent entity and N queries to retrieve the child entities. As the number of entities in the database increases, the queries being executed separately can easily affect the performance of the application. This article will demonstrate how N+1 queries occur and their solution through an example in spring boot. The N+1 Query Problem Consider a Content Management System that stores a list of articles per publication....

March 11, 2021 · 3 min · Me