如何在QuerydslBinderCustomizer中测试自定义绑定?

时间:2018-06-12 12:55:26

标签: spring spring-boot spring-data

我创建了以下QuerydslBinderCustomizer

public interface VehicleForQueryRepository extends JpaRepository<VehicleForQuery, String>, QuerydslPredicateExecutor<VehicleForQuery>,
    QuerydslBinderCustomizer<QVehicleForQuery> {

    @Override
    default void customize(QuerydslBindings bindings, QVehicleForQuery root) {
        // ignore case when searching strings
        bindings.bind(String.class).first((SingleValueBinding<StringPath, String>) StringExpression::containsIgnoreCase);

        // search exactly for a provided value
        bindings.bind(root.id).first(SimpleExpression::eq);
        bindings.bind(root.driveId).first(SimpleExpression::eq);
        bindings.bind(root.costCenter).first(SimpleExpression::eq);

        // search for string starting with a provided value
        bindings.bind(root.licensePlateNumber).first(StringExpression::startsWithIgnoreCase);
        bindings.bind(root.normalizedLicensePlateNumber).first(StringExpression::startsWithIgnoreCase);

        // search for all vehicles of given customer numbers
        bindings.bind(root.kunNr).all((path, value) -> Optional.of(path.in(value)));

        // search for ranges of warning count
        bindings.bind(root.warningCountFrom).first((path, value) -> root.warningCount.goe(value));
        bindings.bind(root.warningCountTo).first((path, value) -> root.warningCount.loe(value));

        // search for ranges of notification count
        bindings.bind(root.notificationCountFrom).first((path, value) -> root.notificationCount.goe(value));
        bindings.bind(root.notificationCountTo).first((path, value) -> root.notificationCount.loe(value));
    }
}

我怎样才能生成&#34; (然后测试它)得到的谓词例如控制器中有@QuerydslPredicate吗?

我的想法是使用@DataJpaTest

@Test
public void findAllShouldIgnoreCaseForSearchOfStringValues() {
    entityManager.persistAndFlush(new VehicleForQuery(1L, UUID.randomUUID().toString(), "0000", "TE-ST 1", "TEST1"));

    BooleanExpression predicate = /* ? */

    Page<VehicleForQuery> result = repository.findAll(predicate, PageRequest.of(0, 20));
    assertThat(result.getTotalElements()).isOne();
}

当然,我可以使用生成的QVehicleForQuery手动创建谓词,但它不会测试上面定义的自定义。

0 个答案:

没有答案