Unit Testing Helidon Like A Pro using Mockito

January 16, 2021 | 1 minute read
Text Size 100%:

Writing JUnit5 unit tests in Helidon is easy with the usage of helidon-microprofile-tests-junit5 dependency. It starts a separate Helidon microprofile server for you to run your tests using @HelidonTest annotation. 

But testing your services (which includes beans which accesses your database layer) is not so straightforward as we need to mock the data access beans without having it so start firing database connections whether H2 or Oracle DB. Mockito is one of the most popular mock framework with can be conjunction with JUnit to test service beans. It allows you to create mock objects with relative ease.

You should have a working Helidon MP project before adding the Mockito library. Create a Helidon project by browsing to Helidon MP Quickstart and following the instructions. You can configure mockito dependencies once you have a helidon project working.

First, add mockito dependencies to your POM as following:

You need mockito dependencies mockito-core and mockito-junit-jupiter. Once this step is done, we can create the unit test class under src/test folder.

We create a test class with a test method as following:

You can run this unit tests by typing the below at the project root using a command prompt.

mvn test

Creation of the test class leaves out few details like creation of Payment entity, Payment service class and Payment repository. These are easy to create Java classes which is covered in the Github link given here


Mohan Naidu

Previous Post

Build Apps Fast in Oracle Cloud Infrastructure - Announcing New Oracle APEX Application Development for Low-Code Developers

Todd Bottger | 5 min read

Next Post

Podcast #389: Tech Predictions for 2021

Javed Mohammed | 2 min read