How to wrap org.json4s.JValue in org.asynchttpclient.Response

75 Views Asked by At

I'm refactoring some tests where previously the response was parsed into org.json4s.JValue

I want to pass the response as type org.asynchttpclient.Response as input parameter. In order to distinguish the HTTP code status.

NettyResponse and WebDavResponse are implementations of org.asynchttpclient.Response.

How to wrap org.json4s.JValue into the body of org.asynchttpclient.Response ?

1

There are 1 best solutions below

0
Raymond Chenon On

The key solution when you cannot set the data of the instances. Mock them.

Here, I use Mockito. Add the dependency

    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>x.x.x</version>
      <scope>test</scope>
    </dependency>

In Scala

import org.junit.Test
import org.mockito.Mockito
import org.mockito.Mockito.when

  @Test
  def GivenXThenAssert(): Unit = {

    val jsonStringResponse: String = ""
    // instantiate the class Response
    val httpResponse = Mockito.mock(classOf[org.asynchttpclient.Response])
    // stub the implementation method
    when(httpResponse.getResponseBodyAsStream()).thenReturn(new ByteArrayInputStream(jsonStringResponse.getBytes(StandardCharsets.UTF_8)))

    // THEN
    //
    val processedResponse = process(httpResponse)
    assertEquals(....)
    }


Notice I stubbed the method getResponseBodyAsStream. Why not another ? Because the asserted function process calls this previous method.