0

I need to mock enum class. (Using Mockito and PowerMockito in TestNG) I was searching for solution, and as a result I found many similar answers:

http://ambracode.com/index/show/285802

How to mock an enum singleton class using Mockito/Powermock?

mocking a singleton class How to mock a method in an ENUM class?

In each provided solution I need to add @PrepareForTest adnotation and mock using powermock.

@PrepareForTest( MyEnum.class)
@Test
public void myTest() {

    MyEnumClass mockInstance = PowerMockito.mock(MyEnumClass .class);
    Whitebox.setInternalState(MyEnumClass.class, "INSTANCE", mockInstance);

    PowerMockito.mockStatic(MyEnumClass.class);

//DoReturn/when and so on...
}

But I still do not understand how it suposed to work? If I try it, I will get java.lang.IllegalArgumentException: Cannot subclass final class class

How I can make "MyEnumClass mockInstance = PowerMockito.mock(MyEnumClass .class);" if it is enum and final? I had that problem before i started searching for the result, but every answer looks the same. What am I missing?

Community
  • 1
  • 1
Thamiar
  • 600
  • 7
  • 22

0 Answers0