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

Quarkus-mockito includes mockito-subclass dependency breaking the mocking of final classes on Java 21+ #45025

Open
lwiddershoven opened this issue Dec 10, 2024 · 2 comments
Labels
area/testing kind/bug Something isn't working

Comments

@lwiddershoven
Copy link

Describe the bug

Reference:

Description

Using the Quarkus Mockito dependency as-is I cannot mock a final class because it also includes a dependency to mockito-subclass. Mockito can mock final classes, but with this mock maker it can not (see Mockito issue referenced above).

I am not sure why the Mockito-subclass dependency is used, it might have a purpose or be needed on older JVMs. So I'm not sure what the side effects of the fix are.

Dependencies

  • Mockito 5.14.2 through Quarkus Mockito 3.17.2

Expected behavior

Final classes can be mocked (as Mockito supports that)

Actual behavior

We see an error when running the test.

[ERROR] IssueTest.issue -- Time elapsed: 0.159 s <<< ERROR!
org.mockito.exceptions.base.MockitoException: 

Cannot mock/spy class java.lang.reflect.Method
Mockito cannot mock/spy because :
 - final class
        at IssueTest.issue(IssueTest.java:12)

How to Reproduce?

Full code on example project: https://github.com/lwiddershoven/mockito-issue

Run mvn clean test

Relevant parts of the dependencies:
pom.xml (broken)

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-junit5-mockito</artifactId>
    <scope>test</scope>
</dependency>
@Test
void issue() {
    var good = mock(IssueTest.class); // works fine regardless
    var error = mock(Method.class); // generates error if mockito-subclass is included
}

Output of uname -a or ver

Darwin Mac.lan 24.1.0 Darwin Kernel Version 24.1.0: Thu Oct 10 21:05:23 PDT 2024; root:xnu-11215.41.3~2/RELEASE_ARM64_T6031 arm64

Output of java -version

JVM OpenJDK Runtime Environment Temurin-21.0.5+11 (build 21.0.5+11-LTS)

Quarkus version or git rev

3.17.2

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.9.9 (8e8579a9e76f7d015ee5ec7bfcdc97d26018693

Additional information

This issue can be fixed by changing the pom, as suggested in the Mockito issue where it was originally filed.
I do not know whether that would break something different on older JVMs.

(Also - if mockito is being looked at - do consider looking at https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#mockito-instrumentation and updating the maven-dependency-plugin and maven-surefire-plugin config to get rid of some of the warning messages that keep popping up.)

pom.xml (fixed)

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-junit5-mockito</artifactId>
    <scope>test</scope>
    <exclusions>
        <!-- Fix for: https://github.com/mockito/mockito/issues/3528
             Mockito 5.14 + Java 21 + Maven surefire agent config : Cannot mock final class
        -->
        <exclusion>
            <artifactId>mockito-subclass</artifactId>
            <groupId>org.mockito</groupId>
        </exclusion>
    </exclusions>
</dependency>
@lwiddershoven lwiddershoven added the kind/bug Something isn't working label Dec 10, 2024
Copy link

quarkus-bot bot commented Dec 10, 2024

/cc @geoand (testing)

@geoand
Copy link
Contributor

geoand commented Dec 10, 2024

I am not sure why the Mockito-subclass dependency is used, it might have a purpose or be needed on older JVMs. So I'm not sure what the side effects of the fix are.

#37927 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/testing kind/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants