Microservice Test - yibinericxia/documents GitHub Wiki
Use @ExtendWith(MockitoExtension.class) in the Mockito library to Mock components in lower layer for fast light-weighted unit testing
We can use SpringExtension (for JUnit 5) annotated with @ExtendWith(SpringExtension.class) or SpringRunner (for JUnit 4) annotated with @RunWith(SpringRunner.class) to integrate Spring’s TestContext Framework including JPA and web-related components (@DataJpaTest & @WebMvcTest) for transaction and dependency injection management.
If we need the full Spring application context for integration tests, we should use @SpringBootTest which includes all the beans, configurations, and application embedded servers like Tomcat, Jetty, etc. We can fine-tune its configuration via webEnvironment and properties, combined with @ActiveProfiles if needed. @SpringBootTest includes SpringExtension after Spring Boot 2.2.0
To use JUnit 5 package in Gradle project
testCompile 'org.junit.jupiter:junit-jupiter:5.9.2'
with a task
tasks.withType(Test) {
useJUnitPlatform()
}
For JavaScript/TypeScript projects, Jest testing framework can be helpful. To mock an object with partial properties, the @golevelup/ts-jest is very convenient.
We may apply the approach of mocking database for service tests in unit testing level.
We could use in-memory database, such as H2 database for Java projects, for integration testing, so developers could easily conduct testing at local.
Please see E2E for more details.
SonarQube & JaCoCo are common tools for Java projects and some related properties can be defined in the CI/CD property file or pom.xml for a maven project as below:
<properties>
<jacoco.version>0.8.7</jacoco.version>
<sonar.version>3.9.0</sonar.version>
<sonar.java.coveragePlugin>jacoco</sonar.java.cooveragePlugin>
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
...
</properties>
and plugins can be defined as follows:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>{jacoco.version}</version>
<configuration>
<excludes>
<exclude>**/dir-name/**</exclude
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
</limits>
</rule>
<rule>
<element>PACKAGE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.50</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
The metrics report is in target/site/jacoco/index.html.
Use Apache Bench (ab) to send 20 request & concurrency of 10 (with the payload file input.json via POST)
ab -n 20 -c 10 [-p input.json] endpoint
Use JMeter for more realistic and accurate testing.
Use Grafana to get prometheus metrics data, such as CPU and memory usage, bandwidth, network, etc, to see if horizontal and/or vertical scale needed for performance issue.
If the service stops processing, the dump of stack trace from "kill -3" can be helpful for root cause analysis of a java application.