

This is because we have to create the captor outside the line we use it on, reducing readability. In contrast to the first test, notice how we have to perform an extra assert on the last line to do the same as eq(credentials).įinally, notice how it isn't immediately clear what credentialsCaptor.capture() refers to. Using kotlin and mockito together throws up some interesting problems, I have already looked at some of these. Gradle/Maven dependency DSL examples Simplest example. When(thenticate(credentialsCaptor.capture())).thenReturn(AuthenticationStatus.AUTHENTICATED) ĪssertEquals(credentials, credentialsCaptor.getValue()) Apart from behavior-mocking, Mockito Kotlin provides a reliable way of verifying interactions with mocked dependencies that occurred while executing the. Kotlin + Mockito MockK mockk, every, Annotation, verify MockK Relaxed Mocks, Verify, Capture Static Method, Singleton Installation All you need to get started is just to add a dependency to MockK library. Next, consider the same test using an ArgumentCaptor instead: Credentials credentials = new Credentials("baeldung", "correct_password", "correct_key") Kotlin and Java are very close friend by Elye Mobile App Development Publication Medium Sign In 500 Apologies, but something went wrong on our end. Here we use eq(credentials) to specify when the mock should return an object. When(thenticate(eq(credentials))).thenReturn(AuthenticationStatus.AUTHENTICATED) ĪssertTrue(thenticatedSuccessfully(credentials)) Mockito 1 is only able to mock interactions of non-final classes and methods, whereas Kotlin has final methods and classes as default.


Credentials credentials = new Credentials("baeldung", "correct_password", "correct_key")
