FAQ?
Is there a way to mock an enum?
But it is incomplete, updating it in case someone bumped into here. Directly calling mock () on enum like ProductAvailabilityStatus availabilityStatus = Mockito.mock (ProductAvailabilityStatus.class) will throw error anyways. Mockito 2 supports mocking of enum, but it is experimental feature.
Is powermock a good use case for enums?
PowerMock opens a door to the land of pain; which you do not want to enter. Seriously; I nowadays think that there is only one good use case for enums; and that is to use them as singletons that provide a certain service. And then, I do this:
Does Junit4 support mocking the enum class?
The JUnit4 test runner doesn't seem to support mocking the Enum class. Each Enum constant can be thought of as an instance of an Enum. By injecting a mocked Enum object into each of these instances, Enum mocking is established.
How to inject a mock object into a powermock element?
The sample code uses the PowerMock library Whitebox.setInternalState () to inject a mock object into the Enum element "ELEM1". As a result, SampleEnum.ELEM1 behaves as the set mock object, and SampleEnum.ELEM2 behaves as it is.