Hamcrest number comparison using between

JavaHamcrest

Java Problem Overview


Is there a way in Hamcrest to compare a number within a number range? I am looking for something like this:

assertThat(50L, is(between(12L, 1658L)));

Java Solutions


Solution 1 - Java

An alternative to Jeff's solution is to use both:

assertThat(50L, is(both(greaterThan(12L)).and(lessThan(1658L))));

I think that's quite readable. You also get a good error message in case the check failed:

> Expected: is (a value greater than <50L> and a value less than <1658L>) > got: <50L>

Solution 2 - Java

I don't believe between is part of the core hamcrest matchers, but you could do something like this:

assertThat(number, allOf(greaterThan(min),lessThan(max)));

That's still a little ugly, so you could create a helper method between

assertThat(number, between(min,max))

and between looks like

allOf(greaterThan(min),lessThan(max))

Still not a fantastic solution, but it reads like a hamcrest matcher.

If you can't find one that's publicly available, it would be trivial to write your own between matcher http://code.google.com/p/hamcrest/wiki/Tutorial.

Solution 3 - Java

If you're dealing with integers, you should consider using the closeTo method:

assertThat((double)nclient, is(closeTo(nserver, 1d)));

unfortunately, there's no closeTo for integers, but every 32 bit integer is representable by a double, so you can simply cast them to doubles, and carry on.

This is not exactly equivalent to between, but can sometimes be helpful.

Solution 4 - Java

Another option is to use the Cirneco extension. It has between(), betweenInclusive() and more. These matchers also apply to other objects that implements Comparable (like Date).

Following your example, it will be:

assertThat(50L, between(12L, 1658L));  

and if you want the two bounds to be included:

assertThat(50L, betweenIncluded(12L, 1658L));  

or if you want just one of the bounds to be included:

assertThat(50L, betweenLowerBoundIncluded(12L, 1658L));  
assertThat(50L, betweenUpperBoundIncluded(12L, 1658L));  

You can use the following dependency for a JDK7-compliant project:

<dependency>
  <groupId>it.ozimov</groupId>
  <artifactId>java7-hamcrest-matchers</artifactId>
  <version>0.7.0</version>
</dependency>

or the following if you are using JDK8

<dependency>
  <groupId>it.ozimov</groupId>
  <artifactId>java8-hamcrest-matchers</artifactId>
  <version>0.7.0</version>
</dependency>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsaw303View Question on Stackoverflow
Solution 1 - JavaChristoph LeiterView Answer on Stackoverflow
Solution 2 - JavaJeff StoreyView Answer on Stackoverflow
Solution 3 - JavaElazar LeibovichView Answer on Stackoverflow
Solution 4 - JavaJeanValjeanView Answer on Stackoverflow