Developer Resources
Deploy Spring Java Backend APIs
Build Deploy with Docker
Pre-Requisites
- Install and setup
- Docker Desktop
- Configure to use Kubernetes via Settings : Kubernetes : Enable Kubernetes
Note: On Windows Run CMD
as Administrator. Also for docker compose to work properly, use linux engine, e.g. via below command.
"C:\Program Files\Docker\Docker"\DockerCli.exe -SwitchLinuxEngine
- Tip: On Win: With Docker: High Memory Used by VmmemWSL and Freeing Up Memory
The VmmemWSL is a process running on the background of your computer which support virtual machine usage on your computer.
- Shutdown via CMD - Administrator Mode
wsl --shutdown
Build and Run One Project
- Locate your project
<PROJECTDIR>
is referenced as project folder, locate dirbackend\spring-java\emapi
cd <PROJECTDIR>
dbrest REST APIs
cd app\dbrest
type Dockerfile | docker build -t dbrest .
List the available docker images:
docker image ls -a
Start container and run image:
docker run -it -p9080:9080 dbrest
dbgraphql GraphQL APIs
cd app\dbgraphql
type Dockerfile | docker build -t dbgraphql .
List the available docker images:
docker image ls -a
Start container and run image:
docker run -it -p9070:9070 dbgraphql
Build and Run Both Project: dbrest & dbgraphql
- Check/verify build-file
cd <PROJECTDIR>
docker-compose config
- Build images, create the containers, and start them:
docker-compose up --build
- Stop the containers
docker-compose down
Testing
tests UT Unit Tests, IT Integration Tests
Below are generated in code generation stage,
- tests Unit Tests
- tests Integration Tests
UT Unit Tests
Using JUnit, Mockito, Mock. Annotations @Test
IT Integration Tests
Using JUnit, Mockito, MockMvc. Annotations @SpringBootTest
, @Test
Enabling Tests
Please enable in Builder Studio: Spring Java Project Configure Flags:
- tests UT Unit Tests, IT Integration Tests : Enable ✅
Test source locations:
- tests Unit Tests
emapi\**\test\**\*UTest.java
- tests Integration Tests
emapi\**\test\**\*ITest.java
Then generated tests will be enabled and can be executed in builds. Test reports in XML format are generated by default.
Running Tests
- Run tests : Build project - verify
mvn clean verify
- Generates test execution data
Make sure below values are set in emapi\pom.xml
<!-- skip unit tests custom property -->
<skipUTs>false</skipUTs>
<!-- skip integration tests maven failsafe property -->
<skipITs>false</skipITs>
<!-- skip testNG API tests maven failsafe property for testApi -->
<skipATs>true</skipATs>
Testing Reports HTML
Test Reports HTML can be generated with maven-surefire-report-plugin
via below maven goals, after running tests:
Generate surefire report for unit tests:
mvn surefire-report:report-only -Daggregate=true
- Report is generated at
emapi\target\site\surefire-report.html
Generate failsafe report for integration tests:
mvn surefire-report:failsafe-report-only -Daggregate=true
- Report is generated at
emapi\target\site\failsafe-report.html
Info: Below plugin is used to generate report with surefire-report
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.2.5</version>
</plugin>
tests API Tests - TestNG, WebClient
API testing with TestNG, WebTestClient.
TestNG tests are executed and its report generated in maven build. Test Reports HTML is generated by default.
Below are generated in code generation stage,
- API Tests - TestNG, WebClient
tests API Tests - TestNG, WebClient
Using TestNG, WebClient. Annotations @Test
Including Tests in Generation
Please Include in Builder Studio: Spring Java Project Configure Flags:
- tests API Tests - TestNG, WebClient : Include ✅
Then generated tests will be available and can be manually enabled and executed.
Enabling and Executing Tests
Please note below:
API Tests are organized in separate spring sub-module
testApi
They are generated but need to enable/run manually by following below mentioned steps.
Build project
emapi
with maven goalinstall
- Note:
- If using Versions: Spring Boot 2 and Java 8, then:
- Edit
emapi\pom.xml
, set values as below for build goalinstall
<skipTests>true</skipTests>
Need to run dbrest and dbgraphql APIs in separate windows commands.
- Maven
install
will install/copy snapshot jars to local repository, which will be used when running API tests:
dir /b /s C:\Users\JohnDoe\.m2\repository\com\example\emapi
:
...
C:\Users\JohnDoe\.m2\repository\com\example\emapi\dbgraphql\1.0-SNAPSHOT\dbgraphql-1.0-SNAPSHOT.jar
C:\Users\JohnDoe\.m2\repository\com\example\emapi\dbgraphql\1.0-SNAPSHOT\dbgraphql-1.0-SNAPSHOT.pom
...
C:\Users\JohnDoe\.m2\repository\com\example\emapi\dbrest\1.0-SNAPSHOT\dbrest-1.0-SNAPSHOT.jar
C:\Users\JohnDoe\.m2\repository\com\example\emapi\dbrest\1.0-SNAPSHOT\dbrest-1.0-SNAPSHOT.pom
...
Running Tests
Run tests from project module levels for testApi.
First,
- Edit
emapi\pom.xml
, set values as below
<!-- skip unit tests custom property -->
<skipUTs>false</skipUTs>
<!-- skip integration tests maven failsafe property -->
<skipITs>false</skipITs>
<!-- skip testNG API tests maven failsafe property for testApi -->
<skipATs>false</skipATs>
- dbrestTest - Test source locations:
emapi\testApi\dbrestTest\src\test\java\testapi\*\*RestApiATest.java
- dbgraphqlTest - Test source locations:
emapi\testApi\dbgraphqlTest\src\test\java\testapi\*\*GraphqlApiATest.java
Run tests with project module level maven goal from IDE or command line as below:
cd emapi\testApi
mvn -pl dbrestTest verify
mvn -pl dbgraphqlTest verify
Aggregate API Tests Report:
mvn surefire-report:failsafe-report-only -Daggregate=true
Testing Reports HTML
Test reports in HTML format are generated by default at locations:
- dbrestTest - Test Report HTML:
emapi\testApi\dbrestTest\target\failsafe-reports\index.html
emapi\testApi\dbrestTest\target\failsafe-reports\emailable-report.html
- dbgraphqlTest - Test Report HTML:
emapi\testApi\dbgraphqlTest\target\failsafe-reports\index.html
emapi\testApi\dbgraphqlTest\target\failsafe-reports\emailable-report.html
- Note: If using Spring Boot 2 and Java 8, then location is at:
emapi\testApi\dbgraphqlTest\target\surefire-reports\index.html
Aggregate Test report for both above in HTML format is at location:
emapi\testApi\target\site\failsafe-report.html
Code Analysis - Code Coverage, Code Quality
Do static source code analysis, find code coverage and ensure code quality with SonarQube.
Now available with Project Settings Configuration Selection via Builder Studio.
- Previously available with EasyManage Templates : Backend Templates : Code Coverage Analysis. Pease find it here Backend Templates
SonarQube - Features
- SonarQube a code quality and security solution that integrates into your CI/CD, and enables you to deploy clean code.
- Static Code Analysis | SAST
- Sonar's SAST (static application security testing) engine detects security vulnerabilities in your code. So they can be eliminated before hand.
Generating Code Coverage Locally with JaCoCo, Sonar Scanner
Note: In root level pom.xml
Set properties.
- Edit
emapi\pom.xml
verify/set below properties to false
<sonar.skip>
,<jacoco.skip>
,<skipITs>
,<skipUTs>
- Run maven goal
mvn clean verify
Local Code Coverage Reports are generated.
- A separate coverage report will be generated for each module.
- Also Aggregate Report of all the module-specific reports into one project-level report is generated at:
emapi\coverage\target\site\jacoco-aggregate\index.html
Code Analysis with SonarQube
How to run SonarQube Locally From Docker
Note: Use SonarQube Ver 8.9 for JDK 8 compatibility
docker pull sonarqube:8.9-community
docker run -d --name sonarqube -p 9000:9000 sonarqube:8.9-community
View SonarQube Landing Page and later on Reports
Go to: http://localhost:9000/
First time: Login passowrd change will be required
Use new password in place of
admin
below formvn sonar:sonar
command
How to Analyze Source Code with SonarQube via mvn goal
- Run maven goal
mvn clean verify sonar:sonar -Dsonar.login=admin -Dsonar.password=admin
After completing, view the results will be available on the SonarQube Projects dashboard
- http://localhost:9000
Build, Deploy Misc Help & Tips
View Maven Dependency
View Junit version and other dependencies for spring-boot-starter-test: In IntelliJ you can do this by Ctrl + click onto pom.xml
artifactId
:spring-boot-starter-test
- View Maven Dependency via Run Maven Goals
CAUTION: Re-download of dependencies may take place, while using below commands.
Show the project dependencies
mvn dependency:tree
Show plugin dependency tree for each plugin
mvn dependency:resolve-plugins