Spring boot 2 with Junit 5 and Mockito 2 for unit testing and integration test

Here we will explain to use Junit 5 and Mockito 2 with Spring boot 2 when it comes to unit testing and integration tests .

mockito-junit5-logo3-horiz.png

First if you are interested to read more about Junit 5 and Mockito 2 , please check the following links :

  1. Junit 5: https://junit.org/junit5/
  2. Mockito 2 : https://github.com/mockito/mockito/wiki/What%27s-new-in-Mockito-2

Here we will show to the following :

  1. Gradle configuration for Junit 5 with spring boot 2
  2. How to define and execute unit test with Junit 5 and mockito 2
  3. How to define and execute spring boot integration test with Junit 5

Gradle configuration for Junit 5 with spring boot 2:

To add the needed Junit 5 dependencies , here what you need to add , whole code sample is on GitHub :

How to define and execute unit test with Junit 5 and mockito 2:

To run Junit 5 test case with Mockito2 , we use Juipter extensions support and here we will use mockito extension , the purpose of Junit 5 extensions is to extend the behavior of test classes or methods, and these can be reused for multiple tests.

How to define and execute spring boot integration test with Junit 5:

Now if we want to call the real service not  the mocked one for integration test with Junit 5 , we will use spring extension to support that and now your integration test is running with Junit 5 support as well.

Hoping this cover the starting jump when you want to use Junit 5 in your spring boot application .

The whole code sample is on GitHub

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s