cancel
Showing results for 
Search instead for 
Did you mean: 

mocking enum in junit issue

Former Member
0 Kudos

Hello,

I am facing an issue in junit testing when i try to set an enum value to model. I am trying to write unit test for a custom populator. Its throwing nosuchmethodfound exception when i try to set enum to the source. What could be the issue ? I tried various approaches. No luck. Any inputs will be useful.

Cheers, Shen

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Try as below, you have to mock enum first and then return values. final ProductAvailabilityStatus availabilityStatus = Mockito.mock(ProductAvailabilityStatus.class); Mockito.when(product.getAvailabilityStatus()).thenReturn(availabilityStatus); Mockito.when(product.getAvailabilityStatus().getCode()).thenReturn(ProductAvailabilityStatus.AVAILABLE.getCode());

Former Member
0 Kudos

Hello,

Thanks for your answer. I tried the same. But its still showing the same error.

0 Kudos

It is an old answer. 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. It needs to be enabled explicitly. If someone is interested about the steps, plz check the details here https://www.w3spot.com/2022/05/how-to-mock-java-enum-or-final-class-without-powermock.html.