Skip to main content


Showing posts from August, 2021

Junit5 Parametrized Tests

 What are parameterized unit tests and How to write them using Junit5? In this article we will learn about parameterized tests and how to write Junit parameterized tests. The parameterized tests is the concept of running a single test multiple times, but with different parameters and outcomes. The parameterized unit tests allow us to avoid writing same code multiple times. JUnit 5, the newest version of JUnit, adds a slew of new capabilities to make creating developer tests easier. Parameterized tests are one such feature. This feature allows us to repeat a test procedure with varied parameters many times. To start writing parameterized tests, we first need to include `junit-jupiter-params` dependency in our project. For Maven, add below dependency to the project's pom.xml file. <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-params</artifactId> <version>5.7.0</version> <scope>test&l

Test Callback Invocation using Junit5 and Mockito

In this article we will use thenAnswer method of Mockito to invoke a callback method passed to a mock.  Let's consider following classes. Here, ServiceToBeTested class is calling submitCallback() method of ServiceToBeMocked class and passing it a Callback object. The value set in Callback object is latter returned by the method being tested. public class ServiceToBeTested { private final ServiceToBeMocked serviceToBeMocked; public ServiceToBeTested(ServiceToBeMocked serviceToBeMocked) { this.serviceToBeMocked = serviceToBeMocked; } public String methodToBeTested() { Callback callback = new Callback(); serviceToBeMocked.submitCallback(callback); return "Value Received after callback = " + callback.getValue(); } } public class ServiceToBeMocked { public void submitCallback(Callback callback) { //some logic that sets value back in callback.

How to verify values passed to a mocked method?

 In this tutorial we will learn how we can verify values passed to a method. Many times we want to verify values passed to a method while execution of the method being tested. This situation may arise when invoked method has no effect on the calling method. Eg. Publishing an event. Let's take a look at the following example, where the `updateBalance()` method is calling `NotificationService` method to publish an event. public class AccountService { private final NotificationService notificationService; public void updateBalance(String accountNumber, BigDecimal newBalance) { // some calculations notificationService.publish(new AccountUpdatedEvent(accountNumber, newBalance)); } } public class NotificationService { /** * Publish an event to the message broker * * @param event */ public void publish(Event event) { // logic here to write to message broker } } Now

Setup Lombok with Spring Boot

 Lombok is a Java library that takes care of generating lot of this boiler plate code for the developers. Lombok works by plugging into your build process and hooking into Java byte-code generation stage, to add required code based on your specifications. Lombok uses annotations to identify which piece of code needs to be auto generated. Adding Lombok to any project is very simple. You simply have to include the  Lombok package in your build path.  If you are using Spring boot, Lombok version will be resolved for you by Spring. Maven pom.xml <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.3.12.RELEASE</version> <relativepath> </relativepath></parent> <dependencies> <!--> <dependency> <groupid>