Search code examples
unit-testingjpaquerydsl

How to test QueryDsl predicates


I have an utility class I use for my QueryDsl queries. I have, for example the following method:

public static StringExpression emptyIfNull(StringExpression expression) {
    return expression.coalesce("").asString();
}

What I actually want to be sure of is that

  1. The expression evaluates to "" when the original expression was null
  2. The expression evaluates to the original string otherwise

How should I test this? Should I setup a db test with my whole context or is there a simpler way to verify that the correct expressions are added, i.e., that using the utility method gives me a COALESCE(<original>, '') SQL function?


Solution

  • I'd test them in action, if you test the serialization instead you'll be looking at prepared statement templates and your tests might break when Querydsl serialization logic is changed.