title: How to test Hibernate criteria queries without using any database?
body: <p>I'm developing a Java application with lots of complex Hibernate criteria queries. I would like to test these criteria to make sure they are selecting the right, and only the right, objects. One approach to this, of course, is to set up an in-memory database (e.g. HSQL) and, in each test, make a round trip to that database using the criteria and then assert that the query results match my expectations. </p>
  
  <p>But I'm looking for a simpler solution, since Hibernate criteria are just a special kind of logical predicates about Java objects. Thus they could, in theory, be tested without accessing any database at all. For example, assuming that there is a entity called <code>Cat</code>:</p>
  
  <pre><code>class Cat {
      Cat(String name, Integer age){
          this.name = name;
          this.age = age;
      }
      ...
  }
  </code></pre>
  
  <p>I would like to do something like this, to create criteria queries:</p>
  
  <pre><code>InMemoryCriteria criteria = InMemoryCriteria.forClass(Cat.class)
     .add(Restrictions.like("name", "Fritz%"))
     .add(Restrictions.or(
        Restrictions.eq("age", new Integer(0)),
        Restrictions.isNull("age")))
  
  assertTrue(criteria.apply(new Cat("Foo", 0)))
  assertTrue(criteria.apply(new Cat("Fritz Lang", 12)))
  assertFalse(criteria.apply(new Cat("Foo", 12)))
  </code></pre>
  
  <p>The criteria could be used in production code like this:</p>
  
  <pre><code>criteria.getExecutableCriteria(session); //similar to DetachedCriteria
  </code></pre>
  
  <p>Is there any Java library that makes this kind of test possible?</p>