【问题标题】:How to do I start a Spring Boot service before the Serenity integration tests?如何在 Serenity 集成测试之前启动 Spring Boot 服务?
【发布时间】:2020-02-18 14:24:13
【问题描述】:

我用谷歌搜索了很多,但我没有找到合适的解决方案来解决我的问题。所以也许在这里。
我想在构建时通过 REST API 测试一个 Spring Boot 服务。因此,在执行集成测试之前,该服务需要在构建阶段作为独立服务执行。 我可以以某种方式执行它,但不是理想的。

我做了类似this article的事情

我有一个使用这个 pom 的 maven 项目:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>my.project.package</groupId>
    <artifactId>my-project-name</artifactId>
    <version>1.0.1</version>
    <packaging>jar</packaging>

    <name>my-project-name</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> 
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <parallel.tests>3</parallel.tests>
        <serenity.maven.version>2.0.48</serenity.maven.version>
        <cucumber.version>4.8.0</cucumber.version>
        <serenity.version>2.1.2</serenity.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20190722</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.16-beta1</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-cucumber4</artifactId>
            <version>${serenity.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-rest-assured</artifactId>
            <version>${serenity.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-spring</artifactId>
            <version>${serenity.version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M1</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>add-test-sources</id>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>add-test-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>src/it/java</source>
                            </sources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>add-test-resources</id>
                        <phase>generate-test-resources</phase>
                        <goals>
                            <goal>add-test-resource</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>src/it/resources</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <includes>
                        <include>**/*Runner.java</include>
                    </includes>
                    <parallel>classes</parallel>
                    <threadCount>${parallel.tests}</threadCount>
                    <forkCount>${parallel.tests}</forkCount>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>net.serenity-bdd.maven.plugins</groupId>
                <artifactId>serenity-maven-plugin</artifactId>
                <version>${serenity.maven.version}</version>
                <dependencies>
                    <dependency>
                        <groupId>io.cucumber</groupId>
                        <artifactId>cucumber-core</artifactId>
                        <version>${cucumber.version}</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>serenity-reports</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>aggregate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

我为测试执行做了一个入口点:

@RunWith(CucumberWithSerenity.class)
@CucumberOptions(
        features = "src/it/resources/features",
        glue = "my.project.package.integrationtest",
        plugin = {
                "pretty"
        },
        tags = "(not @Ignore) or (not @Manual) or (not @ToBeImplemented)"
)
@ActiveProfiles("integration-test")
public class IntegrationTestRunner {}

我有一个启动应用程序的 StepsBase 类:


@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@ActiveProfiles("integration-test")
@ContextConfiguration(classes = Application.class)
public class StepsBase {
    public static final String HOST = "http://localhost:";

    @LocalServerPort
    private int serverPort;

    public String getBaseUrl() {
        return  HOST + serverPort;
    }
}

我所有的步骤类都像这样扩展了 StepsBase 类

public class SimpleSteps extends StepsBase {

    @Given("this is a sample step")
    .
    .
    .
}

如果我使用maven clean verify 执行服务构建,那么我会得到以下集成测试日志:

这个解决方案的主要问题是第一个场景在实例化step类之前执行Spring Boot服务,这也算入执行时间,如果服务没有启动,测试用例不会受到影响。顺便说一句,Serenity 在每个场景之前都会检查服务是否正在运行。

您知道如何在 Serenity 启动之前启动该服务吗? 还是在第一步执行之前?

【问题讨论】:

    标签: spring-boot maven cucumber integration-testing serenity-bdd


    【解决方案1】:

    如果所有内容都在同一个项目(或 maven 模块)中,您可以使用 Spring Boot Maven 插件 (https://docs.spring.io/spring-boot/docs/current/maven-plugin/usage.html)。示例配置如下所示:

                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>2.1.4.RELEASE</version>
                    <configuration>
                        <classifier>exec</classifier>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>pre-integration-test</id>
                            <goals>
                                <goal>start</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>post-integration-test</id>
                            <goals>
                                <goal>stop</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
        ```
    

    【讨论】:

    • 嗨,约翰!感谢你的回答。我以前使用过这个,但我们尝试找到其他选项,因为我们需要随机端口来启动服务。这个解决方案是否让我们有机会使用随机端口?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 2015-08-02
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    相关资源
    最近更新 更多