如何使用 jUnit 断言检查值是否为浮点数?

huangapple go评论88阅读模式
英文:

How to check whether the values are in Float using jUnit assertion?

问题

我正在尝试自动化一个 API。作为响应,我得到一个键的浮点值。这个值是动态的,所以我想要断言的唯一一件事是检查这个值是否是浮点数。我们如何通过 jUnit5 库来进行断言呢?

英文:

I am trying to automate an API. In response, I am getting float value for one of the keys. The value is dynamic so the only thing I want to assert is to check whether the value is a float number or not. How can we assert that via jUnit5 library?

答案1

得分: 1

你可以尝试使用Float.valueOf()将值解析为浮点数,如果无法解释为浮点数,该函数将抛出异常,测试将失败。类似这样:

@Test
public void isFloat() {
    // 这将抛出异常
    Float.valueOf("definitely_not_a_float");
}
英文:

You could just try to read the value as a float using Float.valueOf(), if it can't be interpreted as a float the function will throw an exception and the test will fail. Something like this:

@Test
public void isFloat() {
    // this will throw an exception
    Float.valueOf("definitely_not_a_float");
}

huangapple
  • 本文由 发表于 2020年5月29日 22:25:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/62088195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定