Dmytro Chasovskyi
Dmytro Chasovskyi

Reputation: 3621

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

I have an app that is a heritage from a few years ago. It has JAX-RS and Spring Boot combined.

I have to unit-test @Controller, which is a JAX-RS class annotated as @Component. I am getting a response OutboundJaxrsResponse. The class itself is sealed and doesn't allow me to do much with it.

Is it possible to translate it into something more testable as I care only about status and reason?

COMMENT:

I don't want to make Response.ok().build() as it neglects why I am testing. I would expect to do something like this as I use JUnit5:

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

I searched for different solutions, but JAX-RS seemed to have different approaches for testing it, but I cannot find a convenient and working one.

UPDATE:

I have defined endpoint this way:

@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()
    }
}

Upvotes: 0

Views: 862

Answers (1)

Dmytro Chasovskyi
Dmytro Chasovskyi

Reputation: 3621

My confusion came as I didn't see any expected fields in the org.glassfish.jersey.message.internal.OutboundJaxrsResponse object such as entity, content, body or similar.

To overcome the issue, in my case I have to use response.statusInfo.reasonPhrase which works as I expect.

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

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

NOTE: statusInfo contains many other useful fields to retrieve info of the response.

Upvotes: 1

Related Questions