hamcrest-querydsl

A library of Hamcrest matchers for Querydsl

License

License

Categories

Categories

Querydsl Data Databases
GroupId

GroupId

com.ibm
ArtifactId

ArtifactId

hamcrest-querydsl
Last Version

Last Version

1.0
Release Date

Release Date

Type

Type

jar
Description

Description

hamcrest-querydsl
A library of Hamcrest matchers for Querydsl
Project URL

Project URL

https://github.com/beloglazov/hamcrest-querydsl
Project Organization

Project Organization

IBM Corporation
Source Code Management

Source Code Management

https://github.com/beloglazov/hamcrest-querydsl

Download hamcrest-querydsl

How to add to project

<!-- https://jarcasting.com/artifacts/com.ibm/hamcrest-querydsl/ -->
<dependency>
    <groupId>com.ibm</groupId>
    <artifactId>hamcrest-querydsl</artifactId>
    <version>1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.ibm/hamcrest-querydsl/
implementation 'com.ibm:hamcrest-querydsl:1.0'
// https://jarcasting.com/artifacts/com.ibm/hamcrest-querydsl/
implementation ("com.ibm:hamcrest-querydsl:1.0")
'com.ibm:hamcrest-querydsl:jar:1.0'
<dependency org="com.ibm" name="hamcrest-querydsl" rev="1.0">
  <artifact name="hamcrest-querydsl" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.ibm', module='hamcrest-querydsl', version='1.0')
)
libraryDependencies += "com.ibm" % "hamcrest-querydsl" % "1.0"
[com.ibm/hamcrest-querydsl "1.0"]

Dependencies

compile (5)

Group / Artifact Type Version
org.hamcrest : hamcrest-all jar 1.3
com.mysema.querydsl : querydsl-collections jar 3.4.0
com.mysema.querydsl : querydsl-sql jar 3.4.0
com.mysema.querydsl : querydsl-jpa jar 3.4.0
com.mysema.querydsl : querydsl-jdo jar 3.4.0

test (1)

Group / Artifact Type Version
junit : junit jar 4.11

Project Modules

There are no modules declared in this project.

hamcrest-querydsl

This library provides Hamcrest matchers for Querydsl. In particular, the following matchers for query results are included:

  • hasResultSize
  • hasColumnRange
  • hasColumnMax
  • hasColumnMin
  • hasColumnContainingAll
  • hasColumnContainingAny

Installation

Just add the following dependency to your pom.xml:

<dependency>
    <groupId>com.ibm</groupId>
    <artifactId>hamcrest-querydsl</artifactId>
    <version>1.0</version>
    <scope>test</scope>
</dependency>

Usage

To use the matchers, add the following static import to your test class:

import static com.ibm.hamcrest.querydsl.Matchers.*;

Examples

The examples are given using the collections back-end (querydsl-collections), but the matchers work seamlessly for the SQL, JPA, and JDO back-ends.

Assume that you have a collection of cats with names and ages:

List<Cat> cats = Arrays.asList(
        new Cat("Kitty", 5),
        new Cat("Smokey", 2),
        new Cat("Princess", 4),
        new Cat("Oreo", 3),
        new Cat("Lucky", 7));

Then, a query to select all the cats is:

Cat c = alias(Cat.class), "cat"); // required for the collections back-end
CollQuery q = from($(c), cats);

hasResultSize

Matches the size of the query results:

assertThat(q, hasResultSize(5));

It can be combined with other matchers:

assertThat(q, hasResultSize(greaterThan(1)));
assertThat(q, hasResultSize(lessThan(10)));
assertThat(q, hasResultSize(not(0)));

The query can have arbitrary conditions:

assertThat(q.where($(c.getAge()).gt(3)), hasResultSize(3));

In case of an assertion error (e.g., assertThat(q, hasResultSize(4));), a meaningful message is printed:

Expected: query results with size of <4>
but: actual size was <5>

hasColumnRange

Matches the range of a column in the query results:

assertThat(q, hasColumnRange($(c.getAge()), 2, 7));

hasColumnMax

Matches the maximum value of a column in the query results:

assertThat(q, hasColumnMax($(c.getAge()), 7));
assertThat(q, hasColumnMax($(c.getAge()), greaterThan(6)));
assertThat(q.where($(c.getAge()).lt(5)), hasColumnMax($(c.getAge()), 4));

hasColumnMin

Matches the minimum value of a column in the query results:

assertThat(q, hasColumnMin($(c.getAge()), 2));
assertThat(q, hasColumnMin($(c.getAge()), greaterThan(1)));
assertThat(q.where($(c.getAge()).gt(3)), hasColumnMin($(c.getAge()), 4));

hasColumnContainingAll

Checks whether a column in the query results contains all of the listed values:

assertThat(q, hasColumnContainingAll($(c.getName()), "Oreo"));
assertThat(q, hasColumnContainingAll($(c.getName()), "Oreo", "Kitty", "Smokey"));
assertThat(q.where($(c.getAge()).lt(6)), hasColumnContainingAll($(c.getName()), "Oreo", "Kitty", "Smokey"));

In addition to varargs, an Iterable of the required values can be passed:

assertThat(q, hasColumnContainingAll($(c.getName()), Arrays.asList("Oreo")));
assertThat(q, hasColumnContainingAll($(c.getName()), Arrays.asList("Oreo", "Kitty", "Smokey")));
assertThat(q.where($(c.getAge()).lt(6)), hasColumnContainingAll($(c.getName()), Arrays.asList("Oreo", "Kitty", "Smokey")));

hasColumnContainingAny

Checks whether a column in the query results contains any of the listed values:

assertThat(q, hasColumnContainingAny($(c.getName()), "Oreo"));
assertThat(q, hasColumnContainingAny($(c.getName()), "Oreo", "Kitty", "Smokey"));
assertThat(q, hasColumnContainingAny($(c.getName()), "Oreo", "Schrodinger's"));
assertThat(q.where($(c.getAge()).lt(4)), hasColumnContainingAny($(c.getName()), "Oreo", "Kitty", "Smokey"));

Similarly to hasColumnContainingAll, this matcher supports Iterables:

assertThat(q, hasColumnContainingAny($(c.getName()), Arrays.asList("Oreo")));
assertThat(q, hasColumnContainingAny($(c.getName()), Arrays.asList("Oreo", "Kitty", "Smokey")));
assertThat(q, hasColumnContainingAny($(c.getName()), Arrays.asList("Oreo", "Schrodinger's")));
assertThat(q.where($(c.getAge()).lt(4)), hasColumnContainingAny($(c.getName()), Arrays.asList("Oreo", "Kitty", "Smokey")));

Suggestions for new matchers are welcome!

Author

The project has been developed by Anton Beloglazov.

For more open-source projects from IBM, head over to (http://ibm.github.io).

Copyright and license

© Copyright IBM Corporation 2014. Distributed under the Apache 2.0 license.

Versions

Version
1.0