java 单元测试查询dsl绑定

oipij1gg  于 2023-03-11  发布在  Java
关注(0)|答案(1)|浏览(114)

我有以下使用查询dsl绑定的存储库

public interface StatusRepository extends
                PagingAndSortingRepository<Status, Long>,
                QuerydslPredicateExecutor<Status>,
                QuerydslBinderCustomizer<QStatus> {
        
            @Override
            default void customize(QuerydslBindings bindings, @NotNull QStatus root) {
                bindings.bind(String.class).first((StringPath path, String value) -> 
 path.likeIgnoreCase(value.replace("_", "[_]")));
            }

我正在为customize方法编写单元测试,遇到了困难

@Test
    public void customize() {
        QuerydslBindings bindings = mock(QuerydslBindings.class);
        QStatus qstatus = mock(QStatus.class);
        QuerydslBindings.TypeBinder mock = 
        PowerMockito.mock(QuerydslBindings.TypeBinder.class);
        when(bindings.bind(String.class)).thenReturn(mock);
    
        statusRepository.customize(bindings, qstatusGroup);
    } 

Cannot mock/spy class org.springframework.data.querydsl.binding.QuerydslBindings$TypeBinder
Mockito cannot mock/spy because :
 - final class

我正在使用powermockito来模拟QuerydslBindings.TypeBinder,但它仍然说它不能被模拟

7kqas0il

7kqas0il1#

检查QuerydslPredicateArgumentResolver Spring如何使用绑定器。
其思想是直接使用绑定器并检查生成的 predicate

@Test
void binding() {
    var predicateBuilder = new QuerydslPredicateBuilder(new DefaultConversionService(), SimpleEntityPathResolver.INSTANCE);
    var type = ClassTypeInformation.from(Status.class);
    var binder = new QuerydslBindingsFactory(SimpleEntityPathResolver.INSTANCE)
            .createBindingsFor(type, StatusCustomizer.class);
    var query = new LinkedMultiValueMap<String, String>();
    query.add("param", "value");
    
    Predicate predicate = predicateBuilder.getPredicate(type, query, binder);

    System.out.println(predicate.toString());
}

相关问题