Break New Ground

Unit Testing Helidon Like A Pro using Mockito

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


Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.