Here we will explain to use Junit 5 and Mockito 2 with Spring boot 2 when it comes to unit testing and integration tests .
First if you are interested to read more about Junit 5 and Mockito 2 , please check the following links :
- Junit 5: https://junit.org/junit5/
- Mockito 2 : https://github.com/mockito/mockito/wiki/What%27s-new-in-Mockito-2
Here we will show to the following :
- Gradle configuration for Junit 5 with spring boot 2
- How to define and execute unit test with Junit 5 and mockito 2
- 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