Android WireMock Junit Rule和Non Junit Java Usage无法工作

问题描述 投票:0回答:1

我想用WireMock Integration为一个Android应用写一个测试.我尝试使用所有3种不同的Wiremock Integration方法--1.用JUnit规则写一个测试2. 非JUnit和一般的Java用法3. 针对独立服务器的WireMock APIs。

我可以用WireMock APIs在独立的服务器上运行我的测试.但是因为我不能用这种方法在不同的端口上动态运行,所以我转向了另外2种方法。

下面是Kotlin的示例代码(带有预定义端口)。

为执行Junit规则

@Rule
@JvmField
var wireMockRule: WireMockRule = WireMockRule(8888)

@Test
fun exampleTest() {
    stubFor(WireMock.get(WireMock.urlPathEqualTo( "/requestURL"))
            .willReturn(WireMock.aResponse().withHeader("Content-Type", "application/json")
                    .withStatus(200).withBody(responseString)))

    // App launch and test assertions

}

对于非JUnit和一般的Java用法

@Before
fun setUp() {
    wireMockServer = WireMockServer(wireMockConfig().port(8888))
    wireMockServer.start()

    // Tried it by both commenting and uncommenting the following line
   // configureFor("localhost", wireMockServer.port())
}

@Test
fun exampleTest() {
    val wireMockClient = WireMock("localhost", wireMockServer.port())

    wireMockClient.register(stubFor(WireMock.get(WireMock.urlPathMatching( "/requestURL"))
            .willReturn(WireMock.aResponse().withHeader("Content-Type", "application/json")
                    .withStatus(200).withBody(responseString))))

    // App launch and test assertions
}

对于这两种方法,我都没有得到我存根的响应。我得到的是一个空的响应。将 "withStatus "字段改为400,看看是否是响应字符串的问题,但我也没有收到错误,这时我才确定它根本没有存根!在分享的示例代码中,我有没有遗漏什么?

在分享的示例代码中,我有什么遗漏的地方吗?

注:使用的Junit版本是Junit4,用Kotlin写测试。

android kotlin junit4 wiremock
1个回答
0
投票

想明白了为什么上面的实现不能用了,我的请求调用默认要到主机和端口10.0.2.2和8080(基于现有的代码),我在本地主机和8888上进行wiremock调用,一旦请求调用的主机和端口被更新,它就开始工作了。

© www.soinside.com 2019 - 2024. All rights reserved.