Testing Strategy
Relevant source files
The following files were used as context for generating this wiki page:
- zheng-upms/zheng-upms-rpc-service/src/test/java/com/zheng/upms/rpc/service/UpmsServiceTest.java
- zheng-demo/zheng-demo-rpc-service/src/test/java/com/zheng/AppTest.java
- zheng-pay/zheng-pay-sdk/src/test/java/com/zheng/pay/sdk/AppTest.java
- zheng-upms/zheng-upms-server/src/test/java/com/zheng/upms/server/UpmsServiceTest.java
- zheng-upms/zheng-upms-rpc-service/pom.xml
- zheng-cms/zheng-cms-admin/pom.xml
- zheng-upms/zheng-upms-dao/pom.xml
The testing strategy in the zheng project is centered around Java-based tests using JUnit and the Spring Test Framework. The approach combines basic unit tests for simple components and more comprehensive integration tests for service layers that interact with the database and other parts of the application context.
A significant aspect of the project's build configuration is the frequent disabling of tests during the standard Maven build cycle. This suggests that tests are likely executed on-demand or as part of a dedicated quality assurance step in a CI/CD pipeline, rather than blocking every local build.
Frameworks and Libraries
The project relies on a standard set of tools for its testing needs.
| Library/Framework | Role | Source Files |
|---|---|---|
| JUnit | The core framework for writing and running tests. Both JUnit 3 (TestCase) and JUnit 4 (@Test) styles are present. | zheng-demo/zheng-demo-rpc-service/src/test/java/com/zheng/AppTest.java, zheng-upms/zheng-upms-rpc-service/src/test/java/com/zheng/upms/rpc/service/UpmsServiceTest.java |
| Spring Test | Used for integration testing to load the Spring ApplicationContext and inject dependencies (services, mappers) into test classes. | zheng-upms/zheng-upms-rpc-service/src/test/java/com/zheng/upms/rpc/service/UpmsServiceTest.java:15-22 |
| Maven Surefire Plugin | The Maven plugin responsible for running tests during the build lifecycle. It is consistently configured to skip tests. | zheng-upms/zheng-upms-rpc-service/pom.xml:112-118, zheng-cms/zheng-cms-admin/pom.xml:102-108 |
Maven Surefire Configuration
Across multiple modules, the maven-surefire-plugin is configured to prevent tests from running by default and to ignore test failures if they do run. This is a critical part of the development workflow.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
Sources: zheng-cms/zheng-cms-admin/pom.xml:102-108, zheng-upms/zheng-upms-dao/pom.xml:65-71
This configuration implies that developers must explicitly enable tests when they want to run them, for example by using a command like mvn clean install -DskipTests=false.
Test Types
Basic Unit Tests
Some modules contain placeholder or very basic unit tests that follow the JUnit 3 pattern of extending TestCase. These tests serve as a structural foundation but do not contain complex logic.
public class AppTest extends TestCase {
// ...
public void testApp() {
assertTrue( true );
}
}
Sources: zheng-demo/zheng-demo-rpc-service/src/test/java/com/zheng/AppTest.java:11-29
Service-Layer Integration Tests
The most comprehensive tests are integration tests for the RPC service layer, particularly within the zheng-upms module. These tests use the Spring Test Framework to create a fully-functional application environment.
Key characteristics of these tests include:
- Spring Context Loading: The
@ContextConfigurationannotation is used to load multiple Spring XML configuration files, bootstrapping the application context, including data sources and service beans. - Dependency Injection: Services and mappers are
@Autowireddirectly into the test class. - Transactional Tests: The
@TransactionConfigurationannotation ensures that each test method runs within a database transaction that is rolled back by default upon completion. This isolates tests and prevents them from leaving test data in the database.
The following diagram illustrates the class structure for a typical integration test.
Example: UpmsServiceTest
This test class demonstrates the integration testing strategy for the User Permission Management System (UPMS).
Test Setup:
The class is configured to run with Spring and load the necessary application context files for the database, listeners, and general application configuration. Transactions are managed by transactionManager and are rolled back by default.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({
"classpath:applicationContext.xml",
"classpath:META-INF/spring/applicationContext-jdbc.xml",
"classpath:META-INF/spring/applicationContext-listener.xml"
})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class UpmsServiceTest {
// ...
}
Test Execution Flow:
The following diagram shows the sequence of events when a test method like index() is executed.
Test Methods:
The test methods cover specific service functionalities:
index(): Tests the creation of a newUpmsUser.selectForPage(): Tests two different pagination methods (selectByExampleForStartPageandselectByExampleForOffsetPage) on theUpmsPermissionService.
@Test
public void selectForPage() {
// Based on page number + page size
UpmsPermissionExample upmsPermissionExample = new UpmsPermissionExample();
upmsPermissionExample.createCriteria()
.andSystemIdEqualTo(1);
List<UpmsPermission> upmsPermissions = upmsPermissionService.selectByExampleForStartPage(upmsPermissionExample, 2, 20);
System.out.println(upmsPermissions.size());
// Based on offset + limit
upmsPermissionExample = new UpmsPermissionExample();
upmsPermissionExample.createCriteria()
.andSystemIdEqualTo(2);
upmsPermissions = upmsPermissionService.selectByExampleForOffsetPage(upmsPermissionExample, 3, 5);
System.out.println(upmsPermissions.size());
}
Summary
The testing strategy for the zheng project is pragmatic, focusing on integration tests for critical service layers while keeping the default build process fast by skipping tests. The use of Spring Test and transactional rollbacks ensures that tests are isolated and repeatable without polluting the database. The presence of basic placeholder tests in some modules indicates that while a testing structure is in place, test coverage may vary between different parts of the application.