Search code examples
javajunitjunit-jupiterwebui

Easy test but mistake: "," or "." difference in calculated and expected values


when I'm going to do some checks e.g. to equal expected value 17.41 and calculated value (triangle area via 3 sides), calculated value shows the same numbers but with comma "," that why test failed.

enter image description here

org.opentest4j.AssertionFailedError:

Expected :17.41

Actual :17,41

What should I do? Sorry, these are my first steps in QA, seems that I don'y know some easy things))

public class TriangleSquareCalculator {
    public TriangleSquareCalculator() {}

    public double getTriangleSquare(int a, int b, int c) throws Exception {
        if ((a | b | c) <= 0) throw new Exception("Сторона треугольника не может быть меньше, либо равно 0");
        else {
            double s = ((a + b + c) / 2.0) * ((a + b + c) / 2.0 - a) * ((a + b + c) / 2.0 - b) * ((a + b + c) / 2.0 - c);
            return Math.sqrt(s);
       }
    }
}

@Test
    @DisplayName("Проверка выброса исключения при длине стороны треугольника <=0")
    void exeptionIsTriangleSidePositive () throws Exception {
        TriangleSquareCalculator result = new TriangleSquareCalculator();
        Assertions.assertThrows(Exception.class, ()-> result.getTriangleSquare(-3,7,8));
    }

    @Test
    @DisplayName("Проверка проверка правильности рассчета")
    void calculationCheck () throws Exception {
        TriangleSquareCalculator result2 = new TriangleSquareCalculator();
        Assertions.assertEquals(17.41,String.format("%.2f", (result2.getTriangleSquare(7,5,9))));
    }
 }

Solution

  • More likely 17.41 (the double) is not considered equal to "17.41" the string.

    JUnit ships with a method specifically for comparing doubles:

    assertEquals(17.41, result2.getTriangleSquare(7, 5, 9), .01);
    

    (or perhaps .01 is a bit too lenient a delta, but you get the idea).

    EDIT: Somehow I was sure that Assertions wasn't JUnit's class, but it is; text edited to be specific to junit.