[英]Spring Data JPA Specifications AND operation of predicates unit test not excepted or covered
I have written unit tests for my class and they seem to run fine.我已经为我的 class 编写了单元测试,它们似乎运行良好。 But the challenge is I can't get full coverage or get the builder.and(...) operation part to execute or be covered.
但挑战是我无法获得全面覆盖或让builder.and(...)操作部分执行或被覆盖。
Any idea what could be going on?知道会发生什么吗? Thank you
谢谢
private static Specification<User> getUserSpecification(List<Specification<User>> specifications) {
return getUserSpecification(specifications);
}
private static Specification<User> getUserSpecification(List<Specification<User>> specifications) {
return (root, query, builder) -> {
query.distinct(true);
return getPredicate(specifications, root, query, builder);
};
}
private static Predicate getPredicate(List<Specification<User>> specifications, Root<EncryptedUser> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
return builder.and(
specifications.stream()
.map(specification -> specification.toPredicate(root, query, builder))
.toArray(Predicate[]::new)
);
}
This is my test:这是我的测试:
def "should_find_user"() {
given:
userRepositoryImpl.repository = Mock(UserRepository)
and:
final user = Mock(User) as Specification
final criteria = new UserSearchCriteria(
filter: 'userFilter',
id: 'id'
)
final orderable = new orderable()
final pageable = new pageable()
when:
final foundUser = userRepositoryImpl.findAll(criteria, orderable, pageable)
then:
1 * userRepositoryImpl.repository.findAll(_ as Specification, _ as Pageable) >> new PageImpl<>([user])
and:
foundUser.content.get(0) == user
}
Thank you @LeonardBrünings for the guidance.感谢@LeonardBrünings 的指导。
So, the mistakes I was making were two:所以,我犯的错误有两个:
The code below worked with 100% coverage, ofcourse bits have been omitted下面的代码以 100% 的覆盖率工作,当然位已被省略
@DatabaseTests
@DataJpaTest
class UserRepositoryTest extends Specification {
@Autowired
private TestEntityManager testEntityManager
@Autowired
private UserRepository userRepository
def "should_find paged_users_using_search_criteria"() {
given:
final id = randomUUID()
and:
final orderable = new Orderable()
final pageable = new PageableFor()
final user = new UserId('an user')
final organization = persistAndDetach(new Organization(id: id, name: 'organization'))
final user = persistAndDetach(new User(
....
// omitted
....
))
final criteria = new SearchCriteria(
....
// omitted
....
)
when:
final foundPage = userRepository.findAll(criteria, orderable, pageable)
then:
foundPage.content[0]== user
}
}
protected <T> T persistAndDetach(T entity) {
final persisted = testEntityManager.persistAndFlush(entity)
testEntityManager.detach(persisted)
return persisted
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.