mockito / mockito-kotlin

Using Mockito with Kotlin
MIT License
3.11k stars 202 forks source link

Mockito cannot mock this class: class pl.inpost.recruitmenttask.presentation.shipmentList.Section$HasId. #489

Open houssemzaier opened 1 year ago

houssemzaier commented 1 year ago

Mockito cannot mock this class: class pl.inpost.recruitmenttask.presentation.shipmentList.Section$HasId.

If you're not sure why you're getting this error, please open an issue on GitHub.

Java : 17 JVM vendor name : Amazon.com Inc. JVM vendor version : 17.0.8+7-LTS JVM name : OpenJDK 64-Bit Server VM JVM version : 17.0.8+7-LTS JVM info : mixed mode, sharing OS name : Windows 10 OS version : 10.0

and here's the class:


import pl.inpost.recruitmenttask.presentation.components.CourierPackageItem
import pl.inpost.recruitmenttask.presentation.components.HeaderItem
import pl.inpost.recruitmenttask.presentation.components.ParcelLockerItem

sealed interface Section {
    sealed class HasId(
        val id: String
    )

    data class SectionHeader(val model: HeaderItem.Model) : Section
    data class SectionItemCourier(val model: CourierPackageItem.Model) : Section,
        HasId(model.number)

    data class SectionItemParcelLocker(val model: ParcelLockerItem.Model) : Section,
        HasId(model.number)
}
p-dmitriy commented 1 year ago

I faced same issue with mocking final class after updating mockito kotlin from 4.1.0 to 5.1.0. I mock class that has such hierarchy:

  1. interface Interface
  2. abstract class BaseClass: Interface
  3. abstract class BaseTypedClass: BaseClass
  4. class ImplClass : BaseTypedClass

When I mock ImplClass I got this error:

Mockito cannot mock this class: class ru.gibdd_pay.app.utils.experimentalFeatureManager.experiments.KaskoTabExperiment.
Can not mock final classes with the following settings :
 - explicit serialization (e.g. withSettings().serializable())
 - extra interfaces (e.g. withSettings().extraInterfaces(...))

When I mock BaseTypedClass<Type> I got this error:

Mockito cannot mock this class: class ru.gibdd_pay.app.utils.experimentalFeatureManager.core.RemoteNewOldUsersExperiment.
If you're not sure why you're getting this error, please open an issue on GitHub.
Java               : 17
JVM vendor name    : Amazon.com Inc.
JVM vendor version : 17.0.8.1+8-LTS
JVM name           : OpenJDK 64-Bit Server VM
JVM version        : 17.0.8.1+8-LTS
JVM info           : mixed mode, sharing
OS name            : Mac OS X
OS version         : 13.5.2
You are seeing this disclaimer because Mockito is configured to create inlined mocks.
You can learn about inline mocks and their limitations under item #39 of the Mockito class javadoc.

At the end of error message I saw this, seems it is a problem: Mismatch of count of formal and actual type arguments in constructor of com.package.ImplClass$classField$1: 0 formal argument(s) 1 actual argument(s)

Successfully fixed this by explicitly specifying type of classField. Before it just was initialized with anonymous object of type BaseClass.

HaliksaR commented 8 months ago

Mockito cannot mock this class: interface

Java               : 21
JVM vendor name    : Amazon.com Inc.
JVM vendor version : 21.0.2+13-LTS
JVM name           : OpenJDK 64-Bit Server VM
JVM version        : 21.0.2+13-LTS
JVM info           : mixed mode, sharing
OS name            : Mac OS X
OS version         : 14.1.2

deps

'kotlinMockito'      : "org.mockito.kotlin:mockito-kotlin:5.2.1",
'mockitoJupiter'     : "org.mockito:mockito-junit-jupiter:5.10.0",

args

jvmArgs("-Dnet.bytebuddy.experimental=true")