Java 将谓词用作单元测试的参数

Java 将谓词用作单元测试的参数,java,unit-testing,spring-boot,mockito,predicate,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" } 然而,我目前正在编写单元测试来测试这个函数的有效性,我很难将谓词的

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

@RequestMapping(method = GET)
function getAll(WebRequest request, @QuerydslPredicate(root = Image.class) Predicate predicate, @PageableDefault Pageable pageable) {
   return "test"
}
然而,我目前正在编写单元测试来测试这个函数的有效性,我很难将谓词的参数传递到这个函数中,以便进行测试。有人能帮忙吗??这是我到目前为止的测试功能:

 @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);
   }
}
@RunWith(MockitoJUnitRunner.class)
公共类图像测试{
私有谓词;
@注射模拟
ImageController ImageController=新的ImageController();
@试验
public void testGetAll()引发异常{
WebRequest-WebRequest=mock(WebRequest.class);
ResponseEntity ResponseEntity=ok(新页面响应(第页));
when(imageController.getAll(Mockito.eq(webRequest)、Mockito.eq(谓词)、any(Pageable.class)))
.然后返回(响应性);
}
}
在本例中,谓词为null,因为我不确定将什么设置为参数,因为您正在使用
any(Pageable.class)
作为最后一个参数

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

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

你能给我们看看你现在要测试的代码吗?@Adam是的!我已经更新了我的描述,谢谢!我似乎得到了一个空指针异常,但在作出此更改之后;此外,它还说“错误放置或误用的参数匹配器”;这与我的imageController类有关吗?请尝试显示整个测试类。。或者尽可能多地说明如何实例化谓词以及如何声明实例化控制器?请参阅上面的新增内容。我已经包含了我当前测试类的所有内容,基于您的代码,您可以轻松地在任何地方访问:any(webRequest.class)、any(Predicate.class)、any(Pageable.class))