Purpose : Mock static methods in spring boot application with mockito-inline.
Reason : Increase code coverage ratio.
1- Add mockito-core, mockito-junit-jupiter and mockito-inline dependencies into pom.xml.
2- Use Mockito.mockStatic method inside a try-with-resources statement.
3- Before starting the application run mvn clean install to generate mapstruct mapper class.
4- If generated mapstruct mapper class is not recognized by IntelliJ IDE then reload all maven projects.
5- Start Spring Boot REST API by running main method containing class CustomerInfoApplication.java in your IDE.
6- Alternatively you can start your Docker container by following the commands below.
NOT : Execute maven command from where the pom.xml is located in the project directory to create Spring Boot executable jar.
$ mvn clean install -U -X
$ mvn spring-boot:run
swagger_ui can be accessed via https secure port 8443 from localhost :
https://localhost:8443/customer-info/swagger-ui/index.html
Java 11 H2 Database Engine spring boot spring boot starter data jpa spring boot starter web spring boot starter test spring boot starter aop spring boot starter actuator spring security web springdoc openapi ui springfox swagger ui querydsl-jpa querydsl-apt hibernate logback maven mapstruct mapstruct-processor hikari connection pool mockito-core mockito-junit-jupiter mockito-inline Docker
NOT : Execute docker commands from where the DockerFile is located.
NOT : Tested on Windows 10 with Docker Desktop Engine Version : 20.10.11
$ docker system prune -a --volumes
$ docker build . --tag demo
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
demo latest 9d4a0ec3294e 6 minutes ago 288MB
$ docker run -p 8443:8443 -e "SPRING_PROFILES_ACTIVE=dev" demo:latest
Method : HTTP.POST
URL : https://localhost:8443/customer-info/customer/save
HTTP Request Body :
{ "name": "name1", "age": 1, "shippingAddress": { "address": { "streetName": "software", "city": "ankara", "country": "TR" } } }
Curl Request :
curl --location --request POST 'https://localhost:8443/customer-info/customer/save' \ --header 'Content-Type: application/json' \ --data-raw '{ "name": "name1", "age": 1, "shippingAddress": { "address": { "streetName": "software", "city": "ankara", "country": "TR" } } }'
Response :
HTTP response code 200
{ "id": 1, "name": "name1", "age": 1, "shippingAddress": { "id": 1, "address": { "id": 1, "streetName": "software", "city": "ankara", "country": "TR" } } }
HTTP Response Headers :
request-id: 68182bbf-996d-4732-a6ff-2c49a90012d1 correlation-id: 68182bbf-996d-4732-a6ff-2c49a90012d1 Vary: Origin Vary: Access-Control-Request-Method Vary: Access-Control-Request-Headers
Method : HTTP.GET
URL : https://localhost:8443/customer-info/customer/list
Request Body :
{}
Curl Request :
curl --location --request GET 'https://localhost:8443/customer-info/customer/list' \ --header 'Content-Type: application/json' \ --header 'Cookie: JSESSIONID=5E6B21C9533643F4A7EE462DCBB3B312' \ --data-raw '{}'
Response :
HTTP response code 200
[ { "id": 1, "name": "name1", "age": 1, "shippingAddress": { "id": 1, "address": { "id": 1, "streetName": "software", "city": "ankara", "country": "TR" } } } ]
HTTP Response Headers :
request-id: 411b4b33-6af5-4f78-b185-4171e779222d correlation-id: 411b4b33-6af5-4f78-b185-4171e779222d Vary: Origin Vary: Access-Control-Request-Method Vary: Access-Control-Request-Headers