简体   繁体   中英

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. But the challenge is I can't get full coverage or get the builder.and(...) operation part to execute or be covered.

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.

So, the mistakes I was making were two:

  1. I was mocking the repository the wrong way instead of using @DataJpaTest and then injecting the repository.
  2. I was creating my generic class wrong

The code below worked with 100% coverage, ofcourse bits have been omitted


    @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
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM