0

我正在尝试使用 Kotlin + RxJava 和 MockWebServer 进行一些集成测试。我是测试方面的新手,我是 Kotlin 学徒。我知道 Mockito 和 final 类的局限性,但我不应该嘲笑我要测试的类,所以我不知道真正的问题在哪里:

错误如下:

Apr 16, 2016 9:59:49 PM okhttp3.mockwebserver.MockWebServer$3 execute
INFO: MockWebServer[54260] starting to accept connections
Apr 16, 2016 9:59:50 PM okhttp3.mockwebserver.MockWebServer$3 acceptConnections
INFO: MockWebServer[54260] done accepting connections: Socket closed

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
   Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.


    at com.cesards.android.xxx.xxx.xxx.cloud.GoogleMapsApiClientShould.setUp(GoogleMapsApiClientShould.kt:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

课程如下:

@RunWith(MockitoJUnitRunner::class)
class GoogleMapsApiClientShould : MockWebServerShould() {

    private lateinit var googleMapsApiClient: GoogleMapsApiClient


    private val searchNearbyPlacesSubject: PublishSubject<List<GooglePlaceDTO>> = PublishSubject.create()

    @Before override fun setUp() {
        super.setUp()

        val okHttpClient = GembasOkHttpClient()
        val customApiClient = CustomApiClient(okHttpClient.okHttpClient, baseEndpoint)
        googleMapsApiClient = GoogleMapsApiClient(customApiClient)

        `when`(googleMapsApiClient.searchNearbyPlaces(LatLng(30.30, 30.30))).thenReturn(searchNearbyPlacesSubject)
    }

    @Test fun sendAcceptAndContentTypeHeaders() {
        enqueueMockResponse()

        searchNearbyPlacesSubject.onNext(listOf())

        assertThatRequestContainsHeader("Accept", "application/json")
    }
}

open class GoogleMapsApiClient(apiClient: CustomApiClient) : GoogleMapsApi 

知道发生了什么吗,伙计们?这让我大吃一惊!!

PS:我知道,现在我毕竟不是在嘲笑依赖关系,但我不认为它是相关的,对吧?

4

0 回答 0