mercredi 21 juin 2017

How to write a unit test for spring specification?

I created a builder class which construct Specifications object. It is used for creating queries for JpaSpecificationExecutor. The builder is used, because I have many parameters which can be null/empty (comes from user for filtering) and I cannot use just Specifications without it:

public class SpecificationBuilder<T> {

    private Specifications<T> specification;

    public SpecificationBuilder() {

    }

    public SpecificationBuilder(final Specification<T> spec) {
        specification = Specifications.where(spec);
    }

    public SpecificationBuilder<T> appendOr(final Specification<T> spec) {
        specification = Specifications.where(spec).or(specification);
        return this;
    }

    public SpecificationBuilder<T> appendAnd(final Specification<T> spec) {
        specification = Specifications.where(spec).and(specification);
        return this;
    }

    public Specification<T> build() {
        return Specifications.where(specification);
    }

}

My problem here is that I don't know how to test it in isolation. Of course I can autowire real repository (on in-memory db) but I want to not involve any other classes for that and test only logic, how OR and AND clauses are created.

Unfortunately in spring API I cannot find any method which will help

Aucun commentaire:

Enregistrer un commentaire