### Question:
QueryBuilderFactoryImpl implements QueryBuilderFactory { @Override public <T> QueryBuilder<T> newQueryBuilder( final Class<T> resultType ) { NotQueryableException.throwIfNotQueryable( resultType ); final ServiceReference<EntityFinder> serviceReference; try { serviceReference = finder.findService( EntityFinder.class ); return new QueryBuilderImpl<>( serviceReference.get(), resultType, null ); } catch( NoSuchServiceTypeException e ) { return new QueryBuilderImpl<>( null, resultType, null ); } } QueryBuilderFactoryImpl( ServiceFinder finder ); @Override QueryBuilder<T> newQueryBuilder( final Class<T> resultType );  }### Answer:
@Test public void givenWhereQueryWhenWhereClauseLimitsToRangeExpectLimitedResult() { final QueryBuilder<TestComposite> qb = queryBuilderFactory.newQueryBuilder( TestComposite.class ); TestComposite template = templateFor( TestComposite.class ); Query<TestComposite> query = qb.where( and( ge( template.b(), 3 ), lt( template.b(), 5 ) ) ).newQuery( composites ); verifyOrder( query, "34" ); }
@Test public void givenPlainQueryWhenFindEntityExpectFirstEntityReturned() { Query<TestComposite> query = queryBuilderFactory.newQueryBuilder( TestComposite.class ).newQuery( composites ); assertThat( query.find().a().get(), equalTo( "A" ) ); assertThat( query.count(), equalTo( 6L ) ); }
@Test public void givenPlainQueryWhenOrderByFirstPropertyExpectOrderedResult() { Query<TestComposite> query = queryBuilderFactory.newQueryBuilder( TestComposite.class ).newQuery( composites ); TestComposite template = templateFor( TestComposite.class ); query.orderBy( orderBy( template.a() ) ); verifyOrder( query, "612345" ); }
@Test public void givenPlainQueryWhenOrderBySecondPropertyExpectOrderedResult() { Query<TestComposite> query = queryBuilderFactory.newQueryBuilder( TestComposite.class ).newQuery( composites ); TestComposite template = templateFor( TestComposite.class ); query.orderBy( orderBy( template.b() ) ); verifyOrder( query, "123456" ); }
@Test public void givenPlainQueryWhenOrderByTwoPropertiesExpectOrderedResult() { Query<TestComposite> query = queryBuilderFactory.newQueryBuilder( TestComposite.class ).newQuery( composites ); TestComposite template = templateFor( TestComposite.class ); query.orderBy( orderBy( template.a() ), orderBy( template.b() ) ); verifyOrder( query, "162345" ); }
@Test public void givenPlainQueryWhenMaxedResultExpectLimitedResult() { Query<TestComposite> query = queryBuilderFactory.newQueryBuilder( TestComposite.class ).newQuery( composites ); query.maxResults( 5 ); verifyOrder( query, "62345" ); }
@Test public void givenPlainQueryWhenFirstResultIsBeyondFirstElementExpectLimitedResult() { Query<TestComposite> query = queryBuilderFactory.newQueryBuilder( TestComposite.class ).newQuery( composites ); query.firstResult( 2 ); verifyOrder( query, "3451" ); }
@Test public void givenPlainQueryWhenFindEntityExpectFirstEntityReturned() { Query<TestComposite> query = queryBuilderFactory.newQueryBuilder( TestComposite.class ).newQuery( composites ); assertEquals( "A", query.find().a().get() ); assertEquals( 6, query.count() ); }