使用ZeroCode对SpringBoot应用进行集成测试


这个源码项目演示了如何基于测试框架JUnitZerocode对spring-boot+spring-data(JPA)和H2 in-memory DB应用进行集成测试,让你的日常生活变得轻松。

在进行集成测试时,请保持简单易用

需要依赖:

<dependency>
    <groupId>org.jsmart</groupId>
    <artifactId>zerocode-rest-bdd</artifactId>
    <version>1.2.x</version> 
</dependency>

Junit单元测试在目录test/java/integrationtests/crudtests下,可单独运行API GET测试:
mvn -Dtest= test e.g. > mvn -Dtest=TestGetOperations test

集成测试在目录:
src/test/java/integrationtests/IntegrationTestSuite.java
可直接运行:
mvn -Dtest=IntegrationTestSuite test

Maven中是通过<goal>integration-test</goal>启动集成测试的:

<plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-failsafe-plugin</artifactId>
      <executions>
          <execution>
              <goals>
                  <goal>integration-test</goal>
                  <goal>verify</goal>
                  <configuration>
                    <includes>
                      <include>integrationtests.IntegrationTestSuite.java</include>
                    </includes>
                 </configuration>
              </goals>
          </execution>
      </executions>
  </plugin>

<include>integrationtests.IntegrationTestSuite.java</include>的代码如下:

@EnvProperty("_${env}")
@TargetEnv(
"application_host.properties")
@TestPackageRoot(
"integration_tests")  //You can point this to any package you need -or- use Junit Suite runner to point to individual test classes
@RunWith(E2eJunitSuiteRunner.class)
public class IntegrationTestSuite {

}

@EnvProperty("_${env}") 允许您针对多个环境运行相同的测试套件,只需提及env名称即可。通过Jenkins传递环境参数,并在CI中动态选择特定于环境的属性文件。
在Jenkins中设置“env = cit”,然后选择“application_host_cit.properties”并运行。设置 -Denv = sit,然后寻找并选择“application_host_sit.properties”并运行。

如果env未提供,则默认为“application_host.properties”,默认情况下通过@TargetEnvs设置

mvn goal在特定环境中通过Jenkins目标运行时配置以下内容,例如 -
对于CI:mvn clean install -Denv = ci
对于SIT:mvn clean install -Denv = sit
并确保:resources_host_cit.properties和application_host_sit.properties等在资源文件夹或类路径中可用

在本地测试:

如何在Local上验证@EnvProperty(“_ $ {env}”)测试?
在不同的端口上运行2个应用程序JAR实例
java -jar -Dserver.port = 9090 SpringbootRestInMemoryDB-1.0.0-SNAPSHOT.jar
java -jar -Dserver.port = 7070 SpringbootRestInMemoryDB-1.0.0-SNAPSHOT.jar
针对环境配置文件运行IntegrationTestSuite
mvn -Denv = cit -Dtest = IntegrationTestSuite测试
(针对application_host_cit.properties运行测试。验证来自在9090端口上运行的JAR的日志。)
mvn -Denv = sit -Dtest = IntegrationTestSuite测试
(针对application_host_sit.properties运行测试。验证来自在7070端口上运行的JAR的日志。)
mvn -Dtest = IntegrationTestSuite测试
(针对在默认端口8080上运行的application_host.properties运行测试。)


@RunWith(E2eJunitSuiteRunner.class)
启动SpringBoot应用程序,然后逐个触发测试。

报告
在/ target文件夹下查找详细的测试报告(zerocode-junit-interactive-fuzzy-search.html