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

问题描述 投票:1回答:1

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

@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,因为我不确定要设置什么作为参数

java unit-testing spring-boot mockito predicate
1个回答
1
投票

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

您需要将Mockito.eq()用于其他两个参数:

when(imageController.getAll(Mockito.eq(webRequest), Mockito.eq(predicate), any(Pageable.class)))
  .thenReturn(responseEntity);
© www.soinside.com 2019 - 2024. All rights reserved.