IntelliJ test fails

Hi I’m trying to test my Calculator.java class with a test case but I get this error :

java.lang.AssertionError: expected:<3> but was:[email protected] Expected :3 Actual :[email protected]

This is the test case I wrote

public class CalculatorTest {
    @Test
    public void testCalculator(){
        Calculator calculator = new Calculator();
        calculator.calculate(5,2,'-');
        Assert.assertEquals(3,calculator);

    }

And this is my calculator class

package main;

public class Calculator {



    public Double calculate(double first, double second, char operator) {
        double result;

        switch (operator) {
        case '+':
            result = first + second;
            break;

        case '-':
            result = first - second;
            break;

        case '*':
            result = first * second;
            break;

        case '/':
            result = first / second;
            break;

        // operator doesn't match any case constant (+, -, *, /)
        default:
            System.out.printf("Error! operator is not correct");
            return null;
        }
        return result;
    }

}

Answer

You have two problems:

  • Wrong actual value

  • Wrong double comparison in case of floating points

     Calculator calculator = new Calculator();
     double value = calculator.calculate(5,2,'-'); // use result from calculation
     Assert.assertTrue(Math.abs(3 - value) <= 0.000001);