英文:
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");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论