簡體   English   中英

如何在 org.asynchttpclient.Response 中包裝 org.json4s.JValue

[英]How to wrap org.json4s.JValue in org.asynchttpclient.Response

我正在重構一些以前將響應解析為org.json4s.JValue的測試

我想將響應作為org.asynchttpclient.Response類型作為輸入參數傳遞。 為了區分HTTP代碼狀態。

NettyResponseWebDavResponseorg.asynchttpclient.Response的實現。

如何將org.json4s.JValue org.asynchttpclient.Response主體中?

無法設置實例數據時的關鍵解決方案。 嘲笑他們

在這里,我使用 Mockito。 添加依賴

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

在 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(....)
    }


請注意,我對getResponseBodyAsStream方法進行了存根。 為什么不是另一個? 因為斷言的 function process調用了這個之前的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM