How to use ArgumentMatchers such as Mockito's any () in Kotlin

Mockito in Kotlinany()OrisA()When you use```java.lang.IllegalStateException: Mockito.any() must not be null


 This error does not occur in Null-safe Kotlin because these methods return null (if it is a primitive type, it returns the default value, so this error does not occur).
 Of course, setting the method argument to be verified to Nullable will solve the problem, but you don't want to do that.

 For example, I want to mock the following part and verify the arguments.

```kotlin
transition.onNext(TransitionType.Success())

Since the actual code and the test are different instances at the time of verification, I want to verify whether they are the same class. So I write it like this.

Mockito.doNothing().`when`(mockEventBus).onNext(Mockito.isA(TransitionType.Success::class.java))

This will result in an error.

Solution

If you cast it like the following `nullReturn ()`, it will work.

class TestUtils private constructor() {

    companion object {
        fun <T> isA(type: Class<T>): T {
            Mockito.isA<T>(type)
            return nullReturn()
        }

        fun <T> any(): T {
            Mockito.any<T>()
            return nullReturn()
        }

        private fun <T> nullReturn(): T = null as T
    }
}
Mockito.doNothing().`when`(mockEventBus).onNext(TestUtils.isA(TransitionType.Success::class.java))

For unknown reasons, Kotlin can bypass the compiler null check by casting null, such as `` `null as String. Since I cast it to a NotNull class, is it actually judged as NotNull even if it is null? (At runtime, it will be dropped with TypeCastException```.) And for some reason, casting in Generics also works at runtime.

Recommended Posts

How to use ArgumentMatchers such as Mockito's any () in Kotlin
How to use Lombok in Spring
[Ruby] How to use any? Method
How to use InjectorHolder in OpenAM
How to use classes in Java?
How to try Java preview features (such as Java 15 Record) in IntelliJ
Multilingual Locale in Java How to use Locale
How to use custom helpers in rails
How to use named volume in docker-compose.yml
How to use MySQL in Rails tutorial
How to use environment variables in RubyOnRails
Understand in 5 minutes !! How to use Docker
How to use credentials.yml.enc introduced in Rails 5.2
How to use ExpandableListView in Android Studio
How to use the function implemented in Kotlin Interface introduced in Maven as the default implementation from Java 8
How to call libraries such as JQuery and JQuery UI in Liferay 7 / DXP
[Rails] How to use select boxes in Ransack
How to use "sign_in" in integration test (RSpec)
How to use JQuery in js.erb of Rails6
How to write Java String # getBytes in Kotlin?
[Rails] How to use PostgreSQL in Vagrant environment
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
I want to use Combine in UIKit as well.
[Ruby] How to use standard output in conditional branching
I want to use Clojure's convenient functions in Kotlin
How to use Z3 library in Scala with Eclipse
Understand how to use Swift's JSON Decoder in 3 minutes
How to use CommandLineRunner in Spring Batch of Spring Boot
How to use JDD library in Scala with Eclipse
How to use In-Memory Job repository in Spring Batch
Notes on how to use regular expressions in Java
[Java] How to use Map
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
How to use trained model of tensorflow2.0 with Kotlin / Java
How to use JSON data in WebSocket communication (Java, JavaScript)
[Rails] How to use enum
How to use java Optional
How to use Ruby return
[Rails] How to use enum