首页 > 解决方案 > 在运行空手道测试之前运行位于另一个项目上的 Spring Boot 应用程序?

问题描述

我正在尝试为 Spring Boot 应用程序构建一些空手道测试。这是项目结构:

co-training-backend            (parent)
   |_ co-training-rest         (maven module)
   |_ co-training-rest-karate  (maven module)

如果我运行服务器并启动空手道测试一切正常。现在我想通过从 rest-karate 模块运行服务器来自动化它。这是我的配置:

class CoTrainingTests {

    private static ConfigurableApplicationContext context = null;

    @BeforeAll
    public static void setUp() throws Exception {

        context = SpringApplication.run(RestBootstrap.class, new String[]{});
    }

    @Karate.Test
    Karate testAll() {
        return Karate.run().relativeTo(getClass());
    }

    @AfterAll
    public static void tearDown() throws Exception {
       if(context!=null)
           context.stop();
    }
}

pom.xml

 <artifactId>co-training-rest-karate</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <junit-jupiter.version>5.6.2</junit-jupiter.version>
    <karate.version>0.9.6</karate.version>
    <maven.compiler.version>3.8.1</maven.compiler.version>
</properties>
<dependencies>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.intuit.karate</groupId>
            <artifactId>karate-apache</artifactId>
            <version>${karate.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.intuit.karate</groupId>
            <artifactId>karate-junit5</artifactId>
            <version>${karate.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- this is the spring boot rest project -->
        <dependency>
            <artifactId>co-training-rest</artifactId>
            <groupId>com.co.training</groupId>
            <version>1.0.0-SNAPSHOT</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <testResources>
            <testResource>
                <directory>src/test/java</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </testResource>
        </testResources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.version}</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                </configuration>
            </plugin>
        </plugins>
    </build>

如果我从命令行运行测试,我会遇到这个 Maven 编译器错误:

mvn clean test

[错误] 无法在项目 co-training-rest-karate 上执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.8.1:testCompile (default-testCompile):编译失败 [错误] /home/salto /tutorials/co-training/co-training-backend/co-training-rest-karate/src/test/java/cotraining/CoTrainingTests.java:[3,28] com.co.training.rest 包不存在

但是此类存在于其余依赖项中:

ls /home/salto/.m2/repository/com/co/training/co-training-rest/1.0.0-SNAPSHOT/co-training-rest-1.0.0-SNAPSHOT.jar

/home/salto/.m2/repository/com/co/training/co-training-rest/1.0.0-SNAPSHOT/co-training-rest-1.0.0-SNAPSHOT.jar

任何想法 ?

标签: spring-bootkaratemaven-compiler-plugin

解决方案


您可以使用生命周期的integration-test阶段maven

  1. pre-integration-test : 启动co-training-rest模块

  2. 集成测试:运行karate集成测试

  3. post-integration-test:关闭co-training-rest模块并进行任何其他必要的清理


推荐阅读