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 a final client in a different module results bad sad error #41706

Closed
Thevakumar-Luheerathan opened this issue Nov 16, 2023 · 3 comments
Assignees
Labels
Area/TestFramework Reason/Invalid Issue is invalid. Team/DevTools Ballerina Developer Tooling ( CLI, Test FW, Package Management, OpenAPI, APIDocs ) Type/Bug

Comments

@Thevakumar-Luheerathan
Copy link
Member

Thevakumar-Luheerathan commented Nov 16, 2023

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/");
}

  1. 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);
}

  1. 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
Copy link
Contributor

Dilhasha commented Aug 8, 2024

Please refer #42037 (comment) 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
Copy link
Contributor

Workaround is provided. Hence closing.

Copy link

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.

@Dilhasha Dilhasha added the Reason/Invalid Issue is invalid. label Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/TestFramework Reason/Invalid Issue is invalid. Team/DevTools Ballerina Developer Tooling ( CLI, Test FW, Package Management, OpenAPI, APIDocs ) Type/Bug
Projects
None yet
Development

No branches or pull requests

3 participants