【问题标题】:WebMockServer doesn't response Android EspressoWebMockServer 不响应 Android Espresso
【发布时间】:2021-08-29 19:43:37
【问题描述】:

我已经很好地配置了我的项目,当我调试我的请求时,我看到了 http://10.0.2.2:8080/api/test 所以 CustomTestRunner 和 DI 工作。

我已设置为:

@Before
fun setUp() {
    hiltRule.inject()
    mockWebServer = MockWebServer()
    mockWebServer.enqueue(MockResponse().setResponseCode(200).setBody(getJson(„my_json.json")))
    mockWebServer.start(8080)
}

打开我的活动后,应用程序立即调用服务 api/test,它仅适用于真正的 api,如果我使用 webmockserver 进行模拟,我不会得到我的虚假响应。我的方法 getJson 返回正常的 Json,所以它可以工作。 我还有 9 以上 android 的 http 证书,没有它我得到错误。 任何想法 ?我应该多做点什么?

【问题讨论】:

    标签: android kotlin testing android-espresso okhttp


    【解决方案1】:

    您是在告诉您的应用代码使用模拟服务器 url 来发出请求吗? 就像the MockServer documentation中的例子:

      // Ask the server for its URL. You'll need this to make HTTP requests.
      HttpUrl baseUrl = server.url("/v1/chat/");
    
      // Exercise your application code, which should make those HTTP requests.
      // Responses are returned in the same order that they are enqueued.
      Chat chat = new Chat(baseUrl);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-14
      • 2017-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多