簡體   English   中英

Spring 數據 JPA 謂詞單元測試的規范和操作未排除或涵蓋

[英]Spring Data JPA Specifications AND operation of predicates unit test not excepted or covered

我已經為我的 class 編寫了單元測試,它們似乎運行良好。 但挑戰是我無法獲得全面覆蓋或讓builder.and(...)操作部分執行或被覆蓋。

知道會發生什么嗎? 謝謝


      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)
        );
      }

這是我的測試:

  

    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
      }

感謝@LeonardBrünings 的指導。

所以,我犯的錯誤有兩個:

  1. 我是 mocking 存儲庫的錯誤方式,而不是使用 @DataJpaTest 然后注入存儲庫。
  2. 我正在創建我的通用 class 錯誤

下面的代碼以 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM