This is a demo project about unit test and integration test for Spring Boot Application.
Code: JTSP - 2 - W3D2
- Spring Boot 3
- Spring Data JPA
- H2 DB
- Spring Actuator
- JUnit 5
- Mockito
- JaCoCo
- Spring Profiles
- SonarQube (Optional)
Controller --> Service --> Repository --> H2
Note: for dev and test env, both use H2 vendor but different databases.
application-test.properties
is for test env configuration- Controller unit test should use
MockMvc
and mocked service layer. - Integration test should use
NockMvc
and mocked repository layer since repository access is out of springboot application scope. - Use
mvn clean install
ormvn jacoco:report
to get the report. - Add config in
pom.xml
to exclude some package for unit test coverage.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
<configuration>
<excludes>
<exclude>com/example/springboot3unittestactuator/entity/**</exclude>
<exclude>com/example/springboot3unittestactuator/Exception/**</exclude>
</excludes>
</configuration>
</plugin>
Pull the SonarQube Docker image from the Docker Hub repository:
docker pull sonarqube:latest
Run the SonarQube container:
docker run -d --name sonarqube -p 9000:9000 sonarqube:latest
Add plugin in target project pom.xml
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.9.0.2155</version>
</plugin>
Access localhost:9000 to new project and set up project. Follow instructions and finally there will be a command
mvn clean verify sonar:sonar \
-Dsonar.projectKey=jstp-w3d2 \
-Dsonar.projectName='jstp-w3d2' \
-Dsonar.host.url=http://localhost:9000 \
-Dsonar.token=sqp_dc889445377a673c558d41d8cda78196f28dc461
You will need token generated in SonarQube UI.