问题描述
我正在尝试为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
阶段:
-
集成前测试:启动
co-training-rest
模块 -
集成测试:运行
karate
集成测试 -
集成后测试:关闭
co-training-rest
模块并执行 任何其他必要的清理