Hamcrest Matcher签名者信息与同一软件包中其他类别的签名者信息不匹配

时间:2020-01-29 09:09:47

标签: java spring-boot junit integration-testing hamcrest

我正在尝试使用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 and Export +库: enter image description here

enter image description here

如何设置Eclipse环境以与REST-Assured和Hamcrest一起使用?为什么会引发此异常?

2 个答案:

答案 0 :(得分:1)

我从我的 .p2 插件文件夹中删除了 org.hamcrest.core_1.3.0.v201303031735.jar,它对我有用。

  1. 我在“C:\Users\Dell.p2\pool\plugins”文件夹中搜索了“hamcrest”,并在那里找到了“org.hamcrest.core_1.3.0.v201303031735.jar”。
  2. 我为此路径删除了它。
  3. 尝试运行测试用例,结果在 statusCode() 方法行中顺利通过。

如果有人有更好的解决方案,请提出建议。

答案 1 :(得分:0)

就我而言,问题是通过在我的 pom.xml 的依赖项列表顶部添加对 Hamcrest 的显式依赖项解决的:

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest</artifactId>
    <scope>test</scope>
</dependency>