on 02-28-2018 12:14 PM
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
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());
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
User | Count |
---|---|
5 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.