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

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

Mutables in a Set: Python vs Java

Set is a built-in data type present in both Python and Java but both of these languages handles the elements in a set differently. Though the underlying implementation of set is the same in both languages, Java lets you add mutable objects, such as a list to a set, whereas Python does not. Python Restricts Adding A List To A Set In python, when you try to add an entire list to a set, compiler throws an error:...

January 14, 2021 · 4 min · Me