Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
houssemzaier opened this issue Aug 3, 2023 · 2 comments

Comments

@houssemzaier
Copy link

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
Copy link

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
Copy link

HaliksaR commented Feb 3, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants