ballerina-platform / ballerina-lang

The Ballerina Programming Language
https://ballerina.io/
Apache License 2.0
3.66k stars 751 forks source link

[Bug]: Mocking a final client in a different module results bad sad error #41706

Closed Thevakumar-Luheerathan closed 4 weeks ago

Thevakumar-Luheerathan commented 11 months ago

Description

Following bad sad error occurs when I try to mock final client in a different module

ballerina: Oh no, something really went wrong. Bad. Sad.

We appreciate it if you can report the code that broke Ballerina in
https://github.com/ballerina-platform/ballerina-lang/issues with the
log you get below and your sample code.

We thank you for helping make us better.

[2023-11-16 11:44:41,392] SEVERE {b7a.log.crash} - Cannot invoke "io.ballerina.runtime.internal.values.MapValue.containsKey(Object)" because "globalAnnotMap" is null 
java.lang.NullPointerException: Cannot invoke "io.ballerina.runtime.internal.values.MapValue.containsKey(Object)" because "globalAnnotMap" is null
        at io.ballerina.runtime.internal.AnnotationUtils.processFPValueAnnotations(AnnotationUtils.java:126)
        at luheerathan.test2$test.0.tests.main_test.getMockClient(tests/main_test.bal:11)
        at luheerathan.test2$0046dao.0.dao.intializeClient(dao.bal)
        at luheerathan.test2$0046dao.0.$_init.$gen$$0046$0060init$0062(test2.dao:3)
        at luheerathan.test2$test.0.$_init.$moduleInit(test2)
        at luheerathan.test2$test.0.$_init.$moduleExecute(test2)
        at luheerathan.test2$test.0.$_init.$lambda$$moduleExecute$(test2)
        at io.ballerina.runtime.internal.scheduling.SchedulerItem.execute(SchedulerItem.java:54)
        at io.ballerina.runtime.internal.scheduling.Scheduler.run(Scheduler.java:306)
        at io.ballerina.runtime.internal.scheduling.Scheduler.runSafely(Scheduler.java:273)
        at java.base/java.lang.Thread.run(Thread.java:833)

Steps to Reproduce

1) Create new project as bal new -t main test2 2) Add a new module as bal add dao 3) Add following content to a source file(may be dao.bal) in dao module.

import ballerina/http;

final http:Client clientEndpoint = check intializeClient();

function intializeClient() returns http:Client|error {
    return new ("https://api.chucknorris.io/jokes/");
}

4) Add following code to a test file(may be main_test.bal) of the default module

import test2.dao as _;

import ballerina/http;
import ballerina/test;

@test:Mock {
    moduleName: "test2.dao",
    functionName: "intializeClient"
}
function getMockClient() returns http:Client|error {
    return test:mock(http:Client);
}

5) Execute bal test

Affected Version(s)

2201.8.0 and may be lower versions as well

OS, DB, other environment details and versions

No response

Related area

-> Test Framework

Related issue(s) (optional)

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

Dilhasha commented 2 months ago

Please refer https://github.com/ballerina-platform/ballerina-lang/issues/42037#issuecomment-1918688306 for the explanation of the behaviour.

Workaround:

Move the import statement for the ballerina/test dependency above the import statement for the dependency that's being mocked.

Dilhasha commented 4 weeks ago

Workaround is provided. Hence closing.

github-actions[bot] commented 4 weeks ago

This issue is NOT closed with a proper Reason/ label. Make sure to add proper reason label before closing. Please add or leave a comment with the proper reason label now.

      - Reason/EngineeringMistake - The issue occurred due to a mistake made in the past.
      - Reason/Regression - The issue has introduced a regression.
      - Reason/MultipleComponentInteraction - Issue occured due to interactions in multiple components.
      - Reason/Complex - Issue occurred due to complex scenario.
      - Reason/Invalid - Issue is invalid.
      - Reason/Other - None of the above cases.