Mocking final classes with Java8 when compiling with -parameters flag
See original GitHub issueI 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:
- Created 7 years ago
- Comments:11 (11 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
PS: You can already use the new version, it is released as 2.2.4.
I reported it but those bugs are only filed when they pass QA. I will add the ticket once I receive a confirmation.