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]: No such field exception for client method call in a test function #42693

Open
TharmiganK opened this issue May 6, 2024 · 4 comments · May be fixed by #42709
Open

[Bug]: No such field exception for client method call in a test function #42693

TharmiganK opened this issue May 6, 2024 · 4 comments · May be fixed by #42709
Assignees
Labels
Area/TestFramework Team/DevTools Ballerina Developer Tooling ( CLI, Test FW, Package Management, OpenAPI, APIDocs ) Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime Type/Bug

Comments

@TharmiganK
Copy link
Contributor

Description

Getting no such field exception when I have a client method call inside the test function. The client method has escape characters and has a default parameter.
If I move the same code in the test function to the main function then it worked as expected
It seems like an issue in the generated code for the test function

Steps to Reproduce

  1. Create a ballerina project

  2. Add the following to main.bal

    import ballerina/io;
    client class Client {
    
        resource function get users\-all(string 'order = "asc") returns json {
            return [];
        }
    }
    
    final Client clientEP = new;
    
    public function main() {
        json res = clientEP->/users\-all();
        io:println("obtained response: ", res);
    }
  3. Add the following test function in the tests directory

    import ballerina/io;
    import ballerina/test;
    
    @test:Config {}
    function test() {
        json res = clientEP->/users\-all();
        io:println("obtained response: ", res);
    }
  4. Run bal run - This is working

  5. Run bal test:

    $ bal test
    
    Compiling source
            tharmigan/project:0.1.0
    
    Running Tests
    
            project
    [2024-05-06 08:48:51,532] SEVERE {b7a.log.crash} - $Client_$get$users\-all_order 
    java.lang.NoSuchFieldError: $Client_$get$users\-all_order
            at tharmigan.project$test.0.tests.test.test(tests/test.bal)
            at tharmigan.project$test.0.tests.test_execute-generated_1.test$lambda0$(tests/test_execute-generated_1.bal:4)
            at io.ballerina.runtime.internal.scheduling.SchedulerItem.execute(SchedulerItem.java:54)
            at io.ballerina.runtime.internal.scheduling.Scheduler.run(Scheduler.java:320)
            at io.ballerina.runtime.internal.scheduling.Scheduler.runSafely(Scheduler.java:287)
            at java.base/java.lang.Thread.run(Thread.java:833)
     
    
            test has failed.
    
    
                    [fail] test:
    
                        error("java.lang.NoSuchFieldError: $Client_$get$users\-all_order")
                                    callableName: test$lambda0$ moduleName: tharmigan.project$test.0.tests.test_execute-generated_1 fileName: tests/test_execute-generated_1.bal lineNumber: 4
    
    
    
                    0 passing
                    1 failing
                    0 skipped
    
                    Test execution time : 0.032s
    error: there are test failures

Affected Version(s)

SwanLake Update 8 (2201.8.6)
SwanLake Update 9 (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 May 6, 2024
@nipunayf
Copy link
Contributor

nipunayf commented May 6, 2024

The problem is only reproducible when the client definition is outside of the respective test file. Once the definition is placed along with the test function in the same file, the error is not generated.

@TharmiganK
Copy link
Contributor Author

TharmiganK commented May 6, 2024

The problem is only reproducible when the client definition is outside of the respective test file. Once the definition is placed along with the test function in the same file, the error is not generated.

Yes, the issue arises when we have the generated code for test. There seems to be an inconsistency in the generated byte code for the tests and main, where we call a field to get the default value for this resource method argument.

@Dilhasha
Copy link
Contributor

Dilhasha commented May 6, 2024

@nipunayf Shall we check the generated jar and verify whether this field is there?

@Thevakumar-Luheerathan Thevakumar-Luheerathan self-assigned this May 6, 2024
@Thevakumar-Luheerathan
Copy link
Member

The slash in the resource method is causing the issue. If we remove that slash, it works fine. The following snippets show various instances of generated code.

  • The client is in main.bal

    1. Generated client call in main.bal

      image
    2. Generated client call in test.bal

      image
  • The client is in test.bal

    1. Generated client call in test.bal
      image

@gabilang can you have a look on it?

@gabilang gabilang added the Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime label May 8, 2024
@gabilang gabilang linked a pull request May 8, 2024 that will close this issue
13 tasks
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 ) Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime Type/Bug
Projects
Status: PR Sent
Development

Successfully merging a pull request may close this issue.

6 participants