This code on ServiceImpl
List<EmployeeResponse> employeeResponses = employeeApiClient.getEmployeeByEmpNo(apiSecret, List.of(operationUserId));
Long branchId = Long.parseLong(employeeResponses.get(0).getWorklocationCode());
and, I wanna create the unit test, I already create this,
List<EmployeeResponse> employeeResponses = List.of(mockEmployeeResponse());
Long branchId = Long.parseLong(employeeResponses.get(0).getWorklocationCode());
and, when I use this,
doReturn(List.of(branchId)).when(employeeApiClient).getEmployeeByEmpNo(any(), anyList());
the response is
java.lang.NullPointerException
when I use this,
doReturn(branchId).when(employeeApiClient).getEmployeeByEmpNo(any(), anyList());
the response is
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
Long cannot be returned by getEmployeeByEmpNo()
getEmployeeByEmpNo() should return List
and, when I use this,
when(employeeApiClient.getEmployeeByEmpNo(any(), anyList())).thenReturn(branchId);
the response is
Cannot resolve method 'thenReturn(Long)'
Maybe, any others way to solve this issue, thank you for help.
I'm new on use the unit test (Mock), so I tried to ask about my problem for learning and getting answer.
CodePudding user response:
You are doing right and since employeeApiClient.getEmployeeByEmpNo
is returning List<EmployeeResponse>
you should return the same type
when(employeeApiClient.getEmployeeByEmpNo(any(), anyList())).thenReturn(List.of(mockEmployeeResponse()));
CodePudding user response:
if your method in when()
parameter is return a List of EmployeeResponse, you should put a List of EmployeeResponse too as argument in .thenReturn()
parameter.
example:
List<EmployeeResponse> employeeResponses = List.of(mockEmployeeResponse());
when(employeeApiClient.getEmployeeByEmpNo(any(), anyList())).thenReturn(employeeResponses);
but if you want it return Long
type, you can change when argument to:
Long exampleBranchId = 2L;
when(employeeApiClient.getEmployeeByEmpNo(any(), anyList()).get(anyInt()).getWorklocationCode()).thenReturn(exampleBranchId);