Search code examples
javaeclipsemavendoublejunit4

Why is comparison of double in Eclipse and maven different?


I have program where I am loading properties from file. When property is not present in config file in code I set the property to default value.

In JUnit test I am testing that if property is not set that the value is the default value.

My property is some threshold value in milliseconds which is in string than converted to double and to seconds. Default value is 1000 so 0.001 * 1000 = 1.0.

In my code I have basically this (I am using JUnit 4.8):

double defaultThreshold = 1.0;
double threshold = getThreshold();
Assert.assertEquals(0, Double.compare(defaultThreshold, threshold));

I thought maybe there could be problem with java version, but I tried it in Eclipse with Java 1.6, 1.7 and 1.8 and all passes ok in mvn I use java 1.7.0_71 and it fails on this comparison with:

 expected:<0> but was:<1>

Note: Double.compare returns 0 if both numbers are equal. https://www.tutorialspoint.com/java/lang/double_compare.htm This method returns the value 0 if d1 is numerically equal to d2; a value less than 0 if d1 is numerically less than d2; and a value greater than 0 if d1 is numerically greater than d2.


Solution

  • Problem was not comparison of double but different order of execution of JUnit in console maven and eclipse :/ Reason of problem was that in one case previous test influenced the other test and in other case not because of different order which I would expect would be same but it isn't.