ballerina-lang icon indicating copy to clipboard operation
ballerina-lang copied to clipboard

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

Open TharmiganK opened this issue 1 year ago • 4 comments

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

TharmiganK avatar May 06 '24 03:05 TharmiganK

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.

nipunayf avatar May 06 '24 04:05 nipunayf

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.

TharmiganK avatar May 06 '24 04:05 TharmiganK

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

Dilhasha avatar May 06 '24 04:05 Dilhasha

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?

Thevakumar-Luheerathan avatar May 06 '24 06:05 Thevakumar-Luheerathan

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.

github-actions[bot] avatar Jun 04 '24 04:06 github-actions[bot]