ballerinaballerina-swan-lake

The assertExactEquals is not working as expected for errors Ballerina testing


This happened while I was writing some tests and below is a sample code.

main.bal

public function testError() returns error? {
    io:println("testError executed");
    return error("Error occurred");
}

test.bal

@test:Config {}
function testErrorFunction() {
    test:assertExactEquals(testError(), error("Error occurred"));
}

Result

error {ballerina/test:0}TestError ("Assertion Failed!
 
expected: 'error("Error occurred")'
actual    : 'error("Error occurred")'")
    callableName: createBallerinaError moduleName: ballerina.test.0 fileName: assert.bal lineNumber: 41
    callableName: assertExactEquals moduleName: ballerina.test.0 fileName: assert.bal lineNumber: 170

It seems the assertExactEquals is unable to compare the actual and expected error objects. Note: the actual error contains some additional characters "double quote" and parentheses.


Solution

  • This is the expected behaviour of assertExactEquals. It checks if the two values are exactly referring to the same and are not just similar values. In this case the error returned by testError and the error created within testErrorFunction are two different values with the same error message, hence the assertion fails.

    The confusion here seems to be due to the quotation marks. In the output

    ("Assertion Failed!
     
    expected: 'error("Error occurred")'
    actual    : 'error("Error occurred")'")
    ,
    

    The last double quote is the closing of the double quote opened immediately after the opening parentheses in the first line ("Assertion Failed!.

    Instead of assertExactEquals, you can use assertEquals as follows,

    function testErrorFunction() {
        error? err = testError();
        test:assertTrue(err is error);
        test:assertEquals((<error>err).message(), "Error occurred", "Error message is not same");
    }
    

    You can check the following documentations for more details on this

    [1] https://ballerina.io/learn/test-ballerina-code/write-tests/#use-assertions

    [2] https://central.ballerina.io/ballerina/test/latest#assertEquals