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.

Mock enum with methods fails on Java 17

See original GitHub issue

The following fails on Java 17 and works on 11 and 16:

public enum ExampleEnum {
    TEST {
        public String retrieve() {
            return "test";
        }
    };

    public String getValue() {
        return "21";
    }
}
public class Container {
    private ExampleEnum exampleEnum;
    public Container(ExampleEnum exampleEnum) {
        this.exampleEnum = exampleEnum;
    }

    public String retrieveValue() {
        return exampleEnum.getValue();
    }
}
@ExtendWith(MockitoExtension.class)
public class ExampleEnumTest {

    @Mock
    private ExampleEnum exampleEnum;

    @Test
    public void testEnumWithMethods() {
        Container container = new Container(exampleEnum);
        Mockito.when(exampleEnum.getValue()).thenReturn("42");
        assertEquals("42", container.retrieveValue());
    }
}

On 17 it gives the following error:

org.mockito.exceptions.base.MockitoException:

Mockito cannot mock this class: class com.example.ExampleEnum.

If you're not sure why you're getting this error, please report to the mailing list.


Java               : 17
JVM vendor name    : Oracle Corporation
JVM vendor version : 17-ea+24-2164
JVM name           : OpenJDK 64-Bit Server VM
JVM version        : 17-ea+24-2164
JVM info           : mixed mode, sharing
OS name            : Linux
OS version         : 4.19.76-linuxkit


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.

Underlying exception : org.mockito.exceptions.base.MockitoException: Could not modify all classes [interface java.lang.constant.Constable, class java.lang.Object, interface java.lang.Comparable, interface java.io.Serializable, class java.lang.Enum, class com.example.ExampleEnum]
Caused by: org.mockito.exceptions.base.MockitoException: Could not modify all classes [interface java.lang.constant.Constable, class java.lang.Object, interface java.lang.Comparable, interface java.io.Serializable, class java.lang.Enum, class com.example.ExampleEnum]
Caused by: java.lang.UnsupportedOperationException: class redefinition failed: attempted to change the class NestHost, NestMembers, Record, or PermittedSubclasses attribute

The complete code example can be found here: https://github.com/johanjanssen/JavaUpgrades/tree/main/java17

It can be build with Docker for instance with (17 can be replaced with 11 and 16): docker build -t javaupgrades -f …\Dockerfile --build-arg JDK_VERSION=17 .

Is there some way I can work around this issue?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:28 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
raphwcommented, Jun 12, 2021

Fixed it on master, it’s a documentation error in ASM so I don’t have to wait for a release. I have some other minor issues I have to address but it will be fixed eventually.

0reactions
pburkacommented, Nov 13, 2022

The approach here is to change your program so you don’t have to mock enums.

Say your enum is called Color and it’s got members MAUVE, BEIGE and AMBER, and you want to mock a new color for testing. You can’t, though, because enums are closed sets. To fix this:

  1. Rename the enum to Colors (with an S)
  2. Add an interface Color with whatever methods the enum implements
  3. Add the interface to the enum: enum Colors implements Color
  4. Now you can mock the interface, because interfaces aren’t closed sets

On Wed, Nov 9, 2022, 6:02 PM Namdi @.***> wrote:

@pburka https://github.com/pburka could you kindly give an example of adding the interface to mock the enum (set of singletons in my use case as well).

— Reply to this email directly, view it on GitHub https://github.com/mockito/mockito/issues/2315#issuecomment-1309506714, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA6GRS5FSC73PXVZNMAPZVTWHQUPLANCNFSM46CV2T2A . You are receiving this because you were mentioned.Message ID: @.***>

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mockito: Mocking abstract enum throws exception
The issue is that the current Mockito version (4.6.1) can't mock sealed classes. sealed is a new modifier in java 17, which in...
Read more >
Issue 207 in mockito: how to mock enum class. - Google Groups
What steps will reproduce the problem? 1.create a enum class. 2. try to mock it. 3. What is the expected output? What do...
Read more >
Johan Janssen on Twitter: "The power of opensource: I've built ...
So we're already paving the road for a smooth transition to Java 17. github.com. Mock enum with methods fails on Java 17 ·...
Read more >
Enum (Java Platform SE 7 ) - Oracle Help Center
Method Summary​​ enum classes cannot have finalize methods. Returns the Class object corresponding to this enum constant's enum type. Returns a hash code...
Read more >
JUnit 5 Tutorial: Writing Parameterized Tests - Petri Kainulainen
If our parameterized test takes one enum value as a method parameter, we have to ... 17. 18. 19. import org.junit.jupiter.api.DisplayName;.
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