Mockito确实会因以下错误而死。
@Query("SELECT e FROM Employee e WHERE e.status='A' AND LOWER(e.department.useCode)=LOWER(:flag) AND e.department.status='A' ")
Page<Employee> findAllDetails(@Param("flag") String flag, Pageable pageable);
错误:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-> at com.xxx.service.EmployeeServiceTest.test_findEmployeesPageable(EmployeeServiceTest.java:146)
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
For more info see javadoc for Matchers class.
at com.xxx.service.EmployeeServiceTest.test_findEmployeesPageable(EmployeeServiceTest.java:146)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
代码:
@Override
public Page<EmployeeDto> findEmployeePageable(Pageable pageableReq, String sortParam, String direction, String useCd) {
Pageable pageable = Utils.sort(pageableReq, sortParam, direction);
Page<Employee> response = null;
if(pageable.getSort().isSorted()) {
response = employeeRepository.findAllDetails(code, pageable);
}else {
response = employeeRepository.findAllDetailsNewDetails(useCd, pageable);
}
List<EmployeeDto> employeeResponse = getResponses(response.getContent());
return new PageImpl<>(employeeResponse, response.getPageable(), response.getTotalElements());
}
测试类
@RunWith(PowerMockRunner.class)
@PrepareForTest({PCUtils.class })
public class EmployeeServiceTest {
@InjectMocks
private EmployeeServiceimpl employeeServiceimpl;
@Mock
private Pageable pageableMock;
@Mock
private Page<Employee> employeePage;
@Mock
private EmployeeRepository employeeRepository;
@Mock
private EmployeeRepository programRepositoryMock;
@Mock
private RoleTypeRepository roleTypeRepository;
@Mock
private RoleType roleTypeMock;
@Mock
private Employee employee;
@Mock
private Optional<Employee> employeeOptional;
@Mock
private Employee programMock;
@Mock
private Optional<Employee> programOptionalMock;
@Mock
private EmployeeDto employeeDto;
@Mock
private Sort sortMock;
@Mock
private Exception ex;
@Mock
private Environment env;
@Test
public void test_findEmployeesPageable() {
when(employeeRepository.findAllByStatusAndRoleTypeActive(anyString(), pageableMock)).thenReturn(employeePage);
when(pageableMock.getPageNumber()).thenReturn(1);
when(pageableMock.getSort()).thenReturn(sortMock);
assertNotNull(employeeServiceimpl.findEmployeePageable(pageableMock, anyString(), anyString(), anyString()));
}
}
答案 0 :(得分:1)
您的问题是线路
(1, 2, 3)
这不是 assertNotNull(employeeServiceimpl.findEmployeePageable(pageableMock, anyString(), anyString(), anyString()));
所做的。简而言之,anyString()
用于在您
但是在这里,您试图以完全不同的方式使用anyString()
。看来您正在尝试使Mockito发明参数传递给您的方法。那不是Mockito所做的。
因此,您必须为该方法提供自己的参数。也许像
anyString()
答案 1 :(得分:0)
尝试更换
@Test
public void test_findEmployeesPageable() {
when(employeeRepository.findAllByStatusAndRoleTypeActive(anyString(), pageableMock)).thenReturn(employeePage);
when(pageableMock.getPageNumber()).thenReturn(1);
when(pageableMock.getSort()).thenReturn(sortMock);
assertNotNull(employeeServiceimpl.findEmployeePageable(pageableMock, anyString(), anyString(), anyString()));
}
与
@Test
public void test_findEmployeesPageable() {
when(employeeRepository.findAllByStatusAndRoleTypeActive("", pageableMock)).thenReturn(employeePage);
when(pageableMock.getPageNumber()).thenReturn(1);
when(pageableMock.getSort()).thenReturn(sortMock);
assertNotNull(employeeServiceimpl.findEmployeePageable(pageableMock, "", "", ""));
}
这还大大提高了测试的可读性,因为哪个普通用户将“ Mockito.anyString()”作为方法参数传递?