question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mocking final classes with Java8 when compiling with -parameters flag

See original GitHub issue

I gave the new final class mocking feature a spin by configuring mock-maker-inline in the appropriate place, and by and large it does what it says on the tin: mocking of final classes does work indeed. Awesome!

But I found there’s one unintended side-effect: constructor parameters as retained when compiling with Java 8’s -parameters flag are lost. (We rely on this feature because we use jackson-module-parameter-names.)

The following TestNG test illustrates the issue. Note that test1, test2 and test3 are executed sequentially, in that order. Note also that test1 and test3 are identical. When this class is compiled with -parameters and when mock-maker-inline is enabled, then the first two tests pass (as they should), while the third fails.

import static org.mockito.Mockito.mock;
import static org.testng.Assert.assertEquals;

import org.testng.annotations.Test;

public final class Test {
    static final class Dummy {
        public Dummy(final String dummyArg) {}
    }

    // Passes, because we compile with `-parameters`.
    @Test
    public void test1() throws NoSuchMethodException {
        final String param = Dummy.class.getConstructor(String.class).getParameters()[0].getName();
        assertEquals(param, "dummyArg");
    }

    // Passes, because we have configured `mock-maker-inline`.
    @Test(dependsOnMethods = "test1")
    public void test2() {
        @SuppressWarnings("unused")
        final Dummy d = mock(Dummy.class);
    }

    // Fails, because parameter names got lost.
    @Test(dependsOnMethods = "test2")
    public void test3() throws NoSuchMethodException {
        final String param = Dummy.class.getConstructor(String.class).getParameters()[0].getName();
        assertEquals(param, "dummyArg");
    }
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
raphwcommented, Oct 17, 2016

PS: You can already use the new version, it is released as 2.2.4.

1reaction
raphwcommented, Oct 17, 2016

I reported it but those bugs are only filed when they pass QA. I will add the ticket once I receive a confirmation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mock Final Classes and Methods with Mockito - Baeldung
In this short tutorial, we'll focus on how to mock final classes and methods using Mockito. As with other articles focused on the...
Read more >
How to mock a final class with mockito - java - Stack Overflow
Before Mockito can be used for mocking final classes and methods, it needs to be > configured. We need to add a text...
Read more >
Management & Monitoring - Micronaut Documentation
Using the Micronaut CLI you can create a new Micronaut application in either Groovy, Java, or Kotlin (the default is Java). The following...
Read more >
Gradle tips and recipes - Android Developers
You can also add custom fields to the BuildConfig class from your Gradle build configuration file using the buildConfigField() method and access those...
Read more >
Web on Servlet Stack - Spring
DispatcherServlet</servlet-class> <init-param> ... such that applications can use static methods on RequestContextUtils to look up the WebApplicationContext ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found