Mockito: Have java.lang.IllegalStateException when trying to mock Kotlin class with nullable generic

Created on 26 Jul 2018  路  3Comments  路  Source: mockito/mockito

Hey I'm using

kotlin_version = '1.2.31'
testImplementation "org.mockito:mockito-inline:2.10.0"
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.0.0-RC1"
testImplementation "org.junit.jupiter:junit-jupiter-api:'5.2.0"

in my project.
I have a abstract class like this

abstract class ObservableUseCase<T, in Params> {
    private val disposables = CompositeDisposable()

    abstract fun buildUseCaseObservable(params: Params? = null): Observable<T>

    open fun execute(observer: DisposableObserver<T>, params: Params? = null) {
        val observable = buildUseCaseObservable(params)
        disposables.add(observable.subscribeWith(observer))
    }


    fun shutdown() {
        if (!disposables.isDisposed) disposables.dispose()
    }
}

And implementation of abstract class

class GetDataUseCase constructor(
        private val repository: ProjectsRepository)
    : ObservableUseCase<List<Project>, Nothing>() {
    override fun buildUseCaseObservable(params: Nothing?): Observable<List<Project>> =
            repository.getProjects()
}

The problem: When I try mock my implementation of abstract class I get

java.lang.IllegalStateException: A wildcard does not represent an erasable type: ?

    at net.bytebuddy.description.type.TypeDescription$Generic$OfWildcardType.asErasure(TypeDescription.java:3921)
    at net.bytebuddy.description.type.TypeList$Generic$AbstractBase.asErasures(TypeList.java:267)
    at net.bytebuddy.description.method.MethodDescription$AbstractBase.asTypeToken(MethodDescription.java:709)
    at net.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default$Key$Harmonized.of(MethodGraph.java:857)
    at net.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default$Key$Store.registerTopLevel(MethodGraph.java:1042)
    at net.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default.doAnalyze(MethodGraph.java:575)
    at net.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default.analyze(MethodGraph.java:537)
    at net.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default.analyzeNullable(MethodGraph.java:556)
    at net.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default.doAnalyze(MethodGraph.java:570)
    at net.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default.compile(MethodGraph.java:508)
    at net.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$AbstractBase.compile(MethodGraph.java:408)
    at org.mockito.internal.creation.bytebuddy.MockMethodAdvice.isOverridden(MockMethodAdvice.java:133)
    at com.crashwithnonnull.ObservableUseCase.execute(ObservableUseCase.kt:13)
    at com.crashwithnonnull.ObservableUseCase.execute$default(ObservableUseCase.kt:12)
    at com.crashwithnonnull.UseCaseUser.fetchData(UseCaseUser.kt:9)

Seems like bytebuddy doesn't know about nullable generics or I do something wrong
Also you can find project here

kotlin

Most helpful comment

@garrytrue
Hi, I got the same problem. I change Nothing -> Unit then worked! 馃

All 3 comments

@garrytrue
Hi, I got the same problem. I change Nothing -> Unit then worked! 馃

I do really love this nice-to-have feature to mock a Nothing type in kotlin. Meanwhile, I'm using @thanhbc workaround for this

After a few secounds of searching, I found the solution. In the domain module layer, make sure that the GetDataUseCase use case extend ObservableUseCase, Nothing?>

Nothing -> Nothing?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

miaekim picture miaekim  路  5Comments

cedrik23 picture cedrik23  路  6Comments

bharukaRupesh picture bharukaRupesh  路  3Comments

d1231 picture d1231  路  5Comments

HonoluluHenk picture HonoluluHenk  路  4Comments