使用谓词作为单元测试的参数

时间:2019-02-19 16:20:44

标签: java unit-testing spring-boot mockito predicate

我目前有一个函数,该函数使用数据类型谓词作为该函数的参数之一。见下文:

@RequestMapping(method = GET)
function getAll(WebRequest request, @QuerydslPredicate(root = Image.class) Predicate predicate, @PageableDefault Pageable pageable) {
   return "test"
}

但是,我目前正在编写单元测试来测试此功能的有效性,而且我很难尝试将Predicate的参数传递到此功能中以便进行测试。有人可以帮忙吗?这是我到目前为止的测试功能:

 @RunWith(MockitoJUnitRunner.class)
 public class ImageTest {

 private Predicate predicate;

 @InjectMocks
ImageController imageController = new ImageController(); 

 @Test
public void testGetAll() throws Exception {
    WebRequest webRequest = mock(WebRequest.class);

    ResponseEntity responseEntity = ok(new PagedResponse<>(page));
    when(imageController.getAll(Mockito.eq(webRequest), Mockito.eq(predicate), any(Pageable.class)))
            .thenReturn(responseEntity);
   }
}

在这种情况下,谓词为null,因为我不确定要设置为什么参数

1 个答案:

答案 0 :(得分:0)

您使用any(Pageable.class)作为最后一个参数。

您需要对其他两个参数使用Mockito.eq()

when(imageController.getAll(Mockito.eq(webRequest), Mockito.eq(predicate), any(Pageable.class)))
  .thenReturn(responseEntity);
相关问题