Java Assert Double - NaN

I am trying to claim that my double is NaN. Here is the code snippet:

private Double calculateIt(String input){...} assertEquals(Double.NaN, calculateIt("input text")); 

Code does not compile, Double.NaN is defined as primitive

 public static final double NaN = 0.0d / 0.0; 

To make the statement work, I migrate NaN using the Double object.

 assertEquals(new Double(Double.NaN), calculateIt("input text")); 

Is there a shorter way to do this?

+5
source share
4 answers

You can use:

 boolean isNan = Double.isNaN(calculateIt("input text")); assertTrue(isNan); 

Double.NaN cannot be compared with == ( Double.NaN == Double.NaN will return false ) because NaN is considered special.

Additional Information:

+6
source

assertEquals(Double.NaN, calculateIt(...), 0.0) with assertEquals (double, double, double)

or

assertThat(calculateIt(...), isNan()) with Hamcrest .

best way to do it

What best shows intent? What can you read and easily see what this method is testing?

+1
source

You can try the following:

 assertTrue(Double.compare(Double.NaN, calculateIt("input text"))); 

Hope this helps you.

+1
source

In short, Double.valueOf("NaN") or with static import valueOf("NaN") , but it is basically the same as you already have.

0
source

Source: https://habr.com/ru/post/1243576/


All Articles