java - 如何使用 Mockito 模拟可分页对象?

标签 java junit pagination spring-data-jpa mockito

我正在开发 Spring Boot + Data JPA + Mockito 示例。我已经为 Pagination 存储库方法开发了测试用例,但它给了我以下错误。

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Misplaced or misused argument matcher detected here:

-> at com.xxxx.EmployeeServiceTest.test_findAllEmployeesPaginated(EmployeeServiceTest.java:152)
-> at com.xxxx.EmployeeServiceTest.test_findAllEmployeesPaginated(EmployeeServiceTest.java:152)
-> at com.xxxx.EmployeeServiceTest.test_findAllEmployeesPaginated(EmployeeServiceTest.java:152)
-> at com.xxxx.EmployeeServiceTest.test_findAllEmployeesPaginated(EmployeeServiceTest.java:152)

You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
    when(mock.get(anyInt())).thenReturn(null);
    doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
    verify(mock).someMethod(contains("foo"))

This message may appear after an NullPointerException if the last matcher is returning an object 
like any() but the stubbed method signature expect a primitive argument, in this case,
use primitive alternatives.
    when(mock.get(any())); // bad use, will raise NPE
    when(mock.get(anyInt())); // correct usage use

Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.

    at com.xxxx.EmployeeServiceTest.test_findAllEmployeesPaginated(EmployeeServiceTest.java:152)
    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)

Controller

@GetMapping()
public ResponseEntity<Page<Division>> getAllDivisionsPaginated(
    @RequestParam(required = false, value="page") Integer page, @RequestParam(required = false, value = "size") Integer size,
    @RequestParam(required = false) String sort,
    @RequestParam(required = false) String direction,
    Pageable pageable){

    Page<Division> divisions = divisionService.findAllDivisions(pageable, page, size, sort, direction);
    return new ResponseEntity<>(divisions, HttpStatus.OK);
}

方法:

public Page<Division> findAllEmployees(Pageable pageableReq, Integer page, Integer size, String sortBy, String direction) {
    Pageable pageable = Utils.sort(pageableReq, page, size, sortBy, direction);
    return employeeRepository.findByStatus(StatusEnum.A, pageable);
}

另一种方法:

public static Pageable sort(Pageable pageableReq, Integer page, Integer size, String sortBy, String direction) {
     Pageable pageable = null;
     if (page != null && size != null) {
         if (direction.equalsIgnoreCase("ASC"))
             pageable = PageRequest.of(page, size, Sort.by(sortBy).ascending());
         else if (direction.equalsIgnoreCase("DESC"))
             pageable = PageRequest.of(page, size, Sort.by(sortBy).descending());
     } else {
         pageable = pageableReq;
     }
    return pageable;
}

测试类:

@RunWith(PowerMockRunner.class)
@PrepareForTest({StatusEnum.class})
public class EmployeeServiceTest {

    @Mock
    private Employee employeeMock;

    @InjectMocks
    private EmployeeServiceImpl employeeServiceImpl;

    @Mock
    private EmployeeRepository employeeRepositoryMock;

    @Mock
    private EmployeeDto employeeDtoMock;

    @Mock
    private StatusEnum statusEnum;

    @Mock
    private Exception ex;

    @Mock
    private Pageable pageableMock;

    @Mock
    private Utils utils;

    @Mock
    private Page<Employee> employeePage;

    @Before
    public void setup() {
        mockStatic(StatusEnum.class);       
    }

    @Test
    public void test_findAllEmployeesPaginated() {
        Pageable pageable = PageRequest.of(0, 8);
        when(Utils.sort(pageable, anyInt(), anyInt(), anyString(), anyString())).thenReturn(pageableMock);
        when(employeeRepositoryMock.findByStatus(StatusEnum.A, pageableMock)).thenReturn(employeePage);
    }   
}

最佳答案

  1. 纯 Mockito 无法模拟静态方法。 (您需要使用对象引用 utils 而不是类引用,但您的代码显示该方法是静态的)
    如果你想在这里使用Powermock,你可能会缺少一个mockStatic(Utils.class),但我不确定这是否可以与@Mock注释一起使用。

  2. 您还必须更改排序方法的调用。您必须仅使用匹配器或仅使用实际值。你不能混合它们。

  3. 关于运行器@RunWith(PowerMockRunner.class)。如果您只编写纯 JUnit 4 测试和 Mocktio,则应该考虑使用 Mockito Runner。
    否则,您需要在设置方法中使用 MockitoAnnotations.initMocks(this); 初始化 Mockito。

(您没有使用 PowerMock 标记您的问题,但我认为这可能是不正确的。)

    @Before
    public void setup() throws Exception {
        MockitoAnnotations.initMocks(this);
        mockStatic(StatusEnum.class);       
    }

    @Test
    public void test_findAllEmployeesPaginated() {
        Pageable pageable = PageRequest.of(0, 8);

        mockStatic(Utils.class);
        when(Utils.sort(any(Pageable.class), anyInt(), anyInt(), anyString(), anyString())).thenReturn(pageableMock);
        when(employeeRepositoryMock.findByStatus(StatusEnum.A, pageableMock)).thenReturn(employeePage);
    }

关于java - 如何使用 Mockito 模拟可分页对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57045711/

相关文章:

java - 试图将 List<List<>> 转换为 Object[][]

java - 如何根据 JavaFx 中标签中的文本调整 VBox 和 Label 的大小?

java - 如何使用不同的 JUnit Runner 运行 Spring 单元测试?

java - 使用 JUnit 测试 Spring Controller

java - 如何使用 JPA 2.0 急切加载惰性字段?

java - libgdx 中的居中对齐文本

python - 详细 View 中的分页

Codeigniter:如何对搜索结果进行分页

java - JUNIT runner 无法识别类名

javascript - 如何将html拆分为全屏高度页面?