The short answer - yes.
Old fashioned org.junit.Assert
has a method assertEquals(java.lang.String message, float expected, float actual, float delta)
, and a bunch of a similar method for doubles, overloaded variants wihout the message, and similar implementations of assertArrayEquals
.
If you want to use Hamcrest, the closeTo
matcher is what you're looking for.
EDIT:
To address the question in the comments about percentages - there isn't an out-of-box matcher for this, but you can gimmy-rig something yourself by dividing the two, and making sure they are between the desired ratio and its inverse. To take the example from the OP:
float expected = 5.0;
float actual = 5.5
float ratio = 1.0075;
float inverse = 1/ratio;
float div = actual/expected;
assertThat(div, allOf(greaterThan(inverse), lessThan(ratio)));
It's a bit (well, a lot) clunky, but it should do the trick.