如何测试 JAX-RS org.glassfish.jersey.message.internal.OutboundJaxrsResponse?

How to test JAX-RS org.glassfish.jersey.message.internal.OutboundJaxrsResponse?

我有一个应用程序是几年前的遗产。它结合了 JAX-RS 和 Spring Boot。

我必须对 @Controller 进行单元测试,这是一个注释为 @Component 的 JAX-RS class。 我收到回复 OutboundJaxrsResponse。 class 本身是密封的,不允许我对它做太多事情。

我只关心 statusreason,是否可以将它翻译成更可测试的东西?

评论:

我不想做 Response.ok().build() 因为它忽略了我为什么要测试。我希望在使用 JUnit5 时做这样的事情:

assertEquals("My response", entity(response))
assertEquals(200, response.status)

我搜索了不同的解决方案,但 JAX-RS 似乎有不同的测试方法,但我找不到方便有效的方法。

更新:

我是这样定义端点的:

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{key}")
open override fun getStatus(@PathParam("key") key: String): Response {
    return try {
        Response.status(
            Response.Status.OK.statusCode,
            "This okay"
        ).build()
    } catch (e: Exception) {
        Response.status(
            Response.Status.NOT_FOUND.statusCode,
            "Error, resource not found"
        ).build()
    }
}

我很困惑,因为我没有在 org.glassfish.jersey.message.internal.OutboundJaxrsResponse 对象中看到任何预期的字段,例如 entitycontentbody 或类似的字段。

为了解决这个问题,在我的例子中,我必须使用 response.statusInfo.reasonPhrase,它按我的预期工作。

val ID = "abc"
val response: Response = controller.getInfoBy(ID)

assertEquals("ID: abc", response.statusInfo.reasonPhrase)
assertEquals(200, response.status)

注意:statusInfo 包含许多其他有用的字段来检索响应信息。