I'm new to unit testing and try to understand how mocking works. I got the code as below, where I mock PdRequest.class
using PowerMockRunner
. However, it return Null
for all the variables. I also tried to set the value manually but its the same. Is there anything that I missing? and Is it better if I just create new PdRequest()
and set all the values manually?
What I tried
-Try to use @PrepareForTest
and @ExtendWith
-Try to put initMocks(this)
@RunWith(PowerMockRunner.class)
public class PerformanceServiceImplTest
PdRequest pdRequest = mock(PdRequest.class);
when(pdRequest.getLoginUserId()).thenReturn("42");
when(pdRequest.getYear()).thenReturn("Year");
doNothing().when(pdRequest).setLoginUserId((String) any());
doNothing().when(pdRequest).setCurrMetric((String) any());
doNothing().when(pdRequest).setCurrentLink((BreadCrumLink) any());
doNothing().when(pdRequest).setFullView((String) any());
doNothing().when(pdRequest).setManagerId((String) any());
doNothing().when(pdRequest).setMetric((String) any());
doNothing().when(pdRequest).setPdBoard((String) any());
doNothing().when(pdRequest).setRegionId((String) any());
doNothing().when(pdRequest).setRoleId((String) any());
doNothing().when(pdRequest).setRoleLevel((Integer) any());
doNothing().when(pdRequest).setRoleName((String) any());
doNothing().when(pdRequest).setSalesAbf((String) any());
doNothing().when(pdRequest).setSalesMm((String) any());
doNothing().when(pdRequest).setSalesRsme((String) any());
doNothing().when(pdRequest).setStaffId((String) any());
doNothing().when(pdRequest).setStaffIdList((List<String>) any());
doNothing().when(pdRequest).setTeamId((String) any());
doNothing().when(pdRequest).setYear((String) any());
pdRequest.setCurrMetric("Curr Metric");
pdRequest.setCurrentLink(breadCrumLink);
pdRequest.setFullView("Full View");
pdRequest.setLoginUserId("42");
pdRequest.setManagerId("42");
pdRequest.setMetric("Metric");
pdRequest.setPdBoard("Pd Board");
pdRequest.setRegionId("us-east-2");
pdRequest.setRoleId("42");
pdRequest.setRoleLevel(1);
pdRequest.setRoleName("Role Name");
pdRequest.setSalesAbf("Sales Abf");
pdRequest.setSalesMm("Sales Mm");
pdRequest.setSalesRsme("Sales Rsme");
pdRequest.setStaffId("42");
pdRequest.setStaffIdList(new ArrayList<>());
pdRequest.setTeamId("42");
pdRequest.setYear("Year");
I found a way of getting the value. From https://www.toptal.com/java/a-guide-to-everyday-mockito .