java - 对于预期值使用的正确断言是什么?

标签 java junit

我正在学习使用 Junit。我编写了一个方法,它生成一个严格小于 xX 的数字。

我想使用 Junit 来测试它。不确定要使用哪个断言是基于 xX 的预期,而且我没有看到任何比较断言。

// within the Junit test class
ClassA a = new ClassA();

@Test
void randomTest(){
    assertEquals( ? ,a.getValue(5));
}


public int getValue(int xX){
    // returns an integer less than xX
    return (int) (Math.random() * xX);
}

最佳答案

使用assertTrue喜欢 assertTrue(a.getValue(5)<5);

public static void assertTrue(boolean condition)

Asserts that a condition is true. If it isn't it throws an AssertionError without a message.

关于java - 对于预期值使用的正确断言是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55840691/

相关文章:

java - 在sharedprefs中存储 secret 数据 - android

Java for 循环迭代

java - 模拟抽象类

java - JAX-WS Web 服务线程模型

android - 在 Junit for Android 中使用 FEST-assert 的正确方法?

java - Junit + Ant + Eclipse 单元测试尝试运行但始终返回 1 执行

android-studio - Webview的单元测试

java - @Value 在 junit 5 中为 null

java - 使用 lambda 覆盖默认方法

java - contentType "application/octet-stream"的模拟mvc下载功能抛出 FileNotFound 错误