在Java的单元测试中,我该如何断言一个数字在给定范围内?

35

从Python转到Java。我知道这很基础,但似乎还没有人在这里问过这个问题,而且Google对我也不友好。

在Python中,我会像这样处理Java对象:

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

因为 and 不是 Java 关键字,你需要使用 && - Oliver Charlesworth
1
啊,是的。import static org.junit.Assert.assertTrue 也有帮助。谢谢大家! - klenwell
10个回答

34
我会写:

我会写:

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

但从技术上讲,您只需要:

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

无论哪种方式,一定要写成&&而不是and


20

我会像Jonathan说的那样使用AssertJ,但是使用更简单的断言 :)

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

我认为这是最酷的解决方案 :)


7
你可以使用 Hamcrest 库来实现,这样代码更易读。
assertThat(mynum,greaterThanOrEqualTo(min));

assertThat(mynum,lessThanOrEqualTo(max));

这些行可以通过 allOf 合并,方法如下:

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

6

扩展这个答案:你可以通过allOf结合两者。

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

在 Hamcrest 中的 OR 等价于 anyOf

3
如果您使用AssertJ,代码将变得更加易读
assertThat(mynum).isGreaterThanOrEqualTo(min).isLessThanOrEqualTo(max);

此外,AssertionError 版本比 assertTrue 更好,因为您不需要提供描述,例如:

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

如果您正在使用Java 8和AssertJ 3.0.0,则可以使用lambda来指定它:
assertThat(mynum).matches(actual -> actual >= min && actual <= max);

1
那个错误怎么更易读?10小于或等于42,这只会让人感到困惑。 - clcto
2
我认为与assertTrue和Hamcrest版本相比,这个断言读起来更自然。 - Jonathan

2

使用 AssertJisCloseTo(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 依赖:https://mvnrepository.com/artifact/org.assertj/assertj-core


1

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

末尾的注释是可选的。基本上与Python版本相同,只是使用了&&


1

使用 && 而不是 and;除此之外,你所写的应该可以工作。


1

我也在寻找解决方案,但对现有的解决方案不满意。在JUnit5中,可以使用assertEquals来实现所需的功能。

// Asserts that expected and actual are equal within the given delta. 
assertEquals(expected, actual, delta);

您可以在JUnit5文档中找到更多信息。


嗨,我遇到了与发布的问题类似的情况,并阅读了一些关于使用assertEquals(expected, actual, delta)和“delta”值的文章。似乎delta/epsilon表示实际输出的可接受精度或接受的误差范围。您能否解释一下如何在以下场景中使用此函数。例如:变量“position”的值必须介于MIN_POSITION = 1和MAX_POSITION = 4之间。 - undefined
@hel: 你可以使用assertEquals(2.5, actual, 1.5)。起初可能不太清楚它的意思是actual在1到4之间。但至少你不需要任何其他库或复杂的语法。 - undefined

1

来自真相

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接