简体   繁体   中英

In a Java unit test, how do I assert a number is within a given range?

Coming to Java from Python. I recognize this is pretty basic, but it doesn't seem this has been asked here yet and Google is being coy with me.

In Python, I'd simply do something like this but Java objects:

assertTrue(min <= mynum and mynum <= max);

I'd write:

assertTrue("mynum is out of range: " + mynum, min <= mynum && mynum <= max);

but technically you just need:

assertTrue(min <= mynum && mynum <= max);

Either way, be sure to write && and not and .

I will use AssertJ as Jonathan said, but with simpler assertions :)

 assertThat(mynum).isBetween(min, max);

I think this is the coolest solution :)

Extending on this answer: you can combine the two with allOf .

assertThat(mynum, allOf(greaterThanOrEqualTo(min),lessThanOrEqualTo(max)));

The OR equivalent in Hamcrest is anyOf .

you can use Hamcrest library too ,this is more readable.

assertThat(mynum,greaterThanOrEqualTo(min));

assertThat(mynum,lessThanOrEqualTo(max));

I dont know whether those two lines can be merged.

If you use AssertJ it becomes even more readable:

assertThat(mynum).isGreaterThanOrEqualTo(min).isLessThanOrEqualTo(max);

Plus the AssertionError beats the assertTrue version as you don't need to supply a description, eg:

java.lang.AssertionError: 
Expecting:
 <10>
to be less than or equal to:
 <42> 

If you're using Java 8 and AssertJ 3.0.0, you can use a lambda to specify it:

assertThat(mynum).matches(actual -> actual >= min && actual <= max);

Using AssertJ with isCloseTo(expected, offset) :

BigDecimal maxNegativeOffset = new BigDecimal("0.0004");
BigDecimal actual = new BigDecimal("0.0005");
BigDecimal maxPositiveOffset = new BigDecimal("0.0006");

Offset<BigDecimal> offset = Offset.offset(new BigDecimal("0.0001"));

Assertions.assertThat(actual)
  .isCloseTo(maxNegativeOffset, offset)
  .isCloseTo(maxPositiveOffset, offset);

Maven dependency: https://mvnrepository.com/artifact/org.assertj/assertj-core

assertTrue(min <= mynum && mynum <= max, "not in range");

the comment at the end is optional. Basically the same as the python version, except the && .

Use && rather than and ; other than that, what you wrote should work.

From Truth

import static com.google.common.truth.Truth.assertThat;

assertThat(actualDouble).isWithin(tolerance).of(expectedDouble);

assertThat(0.0999999).isWithin(1E-6).of(0.1d);

assertThat(90d).isWithin(10d).of(100d);   // success
assertThat(105d).isWithin(10d).of(100d);  // success
assertThat(110d).isWithin(10d).of(100d);  // success

assertThat(89d).isWithin(10d).of(100d);   // fails
assertThat(111d).isWithin(10d).of(100d);  // fails
java.lang.AssertionError: <111.0> and <100.0> should have been finite values within <10.0> of each other

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM