Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Mocking function with optional parameter causing NullPointerException #43441

Open
krishnadey30 opened this issue Oct 2, 2024 · 0 comments
Labels
Area/TestFramework Team/DevTools Ballerina Developer Tooling ( CLI, Test FW, Package Management, OpenAPI, APIDocs ) Type/Bug

Comments

@krishnadey30
Copy link

Description

I have encountered a NullPointerException when mocking a function that takes an optional parameter with a default value of () in Ballerina. When calling test:when(mockedFunction).withArguments() and passing () as one of the arguments, I get the following error:

error("java.lang.NullPointerException", message="Cannot invoke "Object.toString()" because the return value of "io.ballerina.runtime.api.values.BIterator.next()" is null")
    callableName: thenReturnFuncExt moduleName: ballerina.test.0 fileName: mock.bal lineNumber: 583
    callableName: thenReturn moduleName: ballerina.test.0.FunctionStub fileName: mock.bal lineNumber: 420
    callableName: testName moduleName: org.product.services$test.0.tests.lib_test fileName: tests/lib_test.bal lineNumber: 1116
    callableName: testName$lambda23$ moduleName: org.product.services$test.0.tests.test_execute-generated_2 fileName: tests/test_execute-generated_2.bal lineNumber: 27

Steps to Reproduce

function optionalJsonMock(json optionalJson = ()) returns string {
    return "This is optionalFunctionMock";
}

@test:Mock {functionName: "optionalJsonMock"}
test:MockFunction optionalJsonMockFn = new ();

@test:Config {}
function testOptionalJsonMock() {
    test:when(optionalJsonMockFn).withArguments(()).thenReturn("This is mocked return value");
    string response = optionalJsonMock();
    test:assertEquals(response, "This is mocked return value");
}

Affected Version(s)

2201.9.0

OS, DB, other environment details and versions

No response

Related area

-> Test Framework

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

@ballerina-bot ballerina-bot added Team/DevTools Ballerina Developer Tooling ( CLI, Test FW, Package Management, OpenAPI, APIDocs ) Area/TestFramework labels Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/TestFramework Team/DevTools Ballerina Developer Tooling ( CLI, Test FW, Package Management, OpenAPI, APIDocs ) Type/Bug
Projects
None yet
Development

No branches or pull requests

2 participants