首页 > 解决方案 > 测试 Spring Boot 休息控制器,无法找到 is() 的包

问题描述

我正在为 Spring Boot 中的 REST 控制器编写测试。是使用 is(),但我的 IDE 并没有提示这是什么包,所以我找不到它。

这是我的测试方法;

@Test
    public void printerIsReady() throws Exception {
        /* Set the contents of status.txt to anything other than 3 */
        String statusFilePath = printerPath + "/status.txt";
        PrinterFileHelper.writeToFile("5", statusFilePath);

        this.mockMvc.perform(get("/printer"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.status", is("READY")));

    }

我在网上找到了很多使用 is() 方法的示例,但是完全不清楚我需要添加什么包才能访问它。

标签: spring-boot

解决方案


我在这里找到了答案How to check String in response body with mockMvc

您需要导入的包是;

import static org.hamcrest.Matchers.*;

我需要将包添加到我的 pom.xml 文件中

<dependency>
    <groupId>org.testinfected.hamcrest-matchers</groupId>
    <artifactId>core-matchers</artifactId>
    <version>1.5</version>
</dependency>

推荐阅读