0

I'm trying to make some integration tests with Kotlin + RxJava and MockWebServer. I'm kind of newbie in terms of testing and I'm Kotlin apprentice. I know about the limitations of Mockito and final classes, but I shouldn't be mocking the class I'm trying to test, so I don't know where is the real problem:

The error is the following:

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)

And the class is the following:

@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")
    }
}

and

open class GoogleMapsApiClient(apiClient: CustomApiClient) : GoogleMapsApi 

Any idea what is going on, guys? This is blowing my mind!!

PS: I know, for now I'm not mocking the dependencies after all, but I don't think it's related, right?

cesards
  • 15,882
  • 11
  • 70
  • 65
  • 2
    You're calling when() with, as argument, a method call on a real object rather than a method call on a mock. Thatcan't work. You need to mock (or spy) GoogleMapsApiClient to be able to do that. – JB Nizet Apr 16 '16 at 21:31
  • Thank you. I thought Mockito helped you to create "real" dependencies, so you also could do it manually. I'm gonna try to use a library to mock the dependancies, because in Kotlin is not possible yet, and let's see if I fix it. Cheers man! – cesards Apr 16 '16 at 21:50
  • Mockito helps you create mock dependencies. And it's possible to use Mockito with Kotlin (but the classes to mock must be open). I suggest you learn what Mockito is all about. – JB Nizet Apr 16 '16 at 21:53
  • I don't want to open my classes. Have you tried PowerMock with Kotlin? I tried this library but It didn't help at all, to be honest: https://github.com/nhaarman/mockito-kotlin – cesards Apr 16 '16 at 22:43
  • @JBNizet Maybe you will add your comment as an answer? – Michael Apr 18 '16 at 09:59

0 Answers0