spring-boot - 在运行空手道测试之前运行位于另一个项目上的 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
任何想法 ?
解决方案
您可以使用生命周期的integration-test
阶段maven
:
pre-integration-test : 启动
co-training-rest
模块集成测试:运行
karate
集成测试post-integration-test:关闭
co-training-rest
模块并进行任何其他必要的清理
推荐阅读
- c++ - 从 macports (macOS) 安装 gcc 8 的 gdb libstdc++ 漂亮打印机中的错误
- git - Git 和开源/社区项目中程序员之间的协调——它是如何工作的?
- node.js - git commit 添加了 2 个新行
- angular - 如何在 html 模板中实现这一点
- sqlite - 关联 2 个表的 SQL 查询
- python - 使 Jupyter 笔记本中的后续状态无效
- sql - 我的计划命令删除了我的所有记录
- unicode - Unicode 字符代表“全屏”?
- python - 属性错误 ::datetime.date 对象没有属性“拆分”
- java - Java在循环中绘制移动矩形