【发布时间】:2020-05-14 18:04:06
【问题描述】:
我正在尝试使用 REST-Assured 和 JUnit5 在我的 Spring Boot 应用程序中编写一些集成测试,但是当我运行以下命令时:
@SpringBootTest(classes = ProductsApplication.class)
class ProductsApiTest {
@Before
public void setup() {
RestAssured.baseURI = "http://localhost:8080/test/api/products";
}
@Test
public void test1() {
ValidatableResponse statusCode = given().when().get().then().statusCode(200);
}
}
出现了一个严重的错误:
java.lang.SecurityException:类“org.hamcrest.Matchers”的签名者 信息与其他类的签名者信息不匹配 同一个包
请看一下我的 pom.xml:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
...
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<scope>test</scope>
</dependency>
...
<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
</dependency>
</dependencies>
<build>
...
</build>
</project>
这里是 Eclipse 项目使用的 Order 和 Export + Libraries:
如何设置 Eclipse 环境以使用 REST-Assured 和 Hamcrest?为什么会抛出这个异常?
【问题讨论】:
-
是来自aspectj 还是来自junit4 的@Before 注释?
-
@SirDiRakaTymurKubai 它来自junit
-
@Aivaras 我看过这篇文章,但不知道当我在类路径上有 Junit(包括 hamcrest)和在 maven(也包括它)中的 spring boot 测试时该怎么做。两者都需要留下,但它们都包括 hamcrest。
-
@StoyanLupov 根据你的 pom.xml 你在你的代码中使用 junit-jupiter(junit5) 对接你有 @Before(junit4)注释而不是 @BeforeEach(junit5)。我不知道您如何运行测试(此 junit4 或 junit5 使用什么框架)以及代码示例中的 imports 是什么
标签: java spring-boot junit integration-testing hamcrest