ballerina-lang
ballerina-lang copied to clipboard
[Bug]: No such field exception for client method call in a test function
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
-
Create a ballerina project
-
Add the following to
main.balimport 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); } -
Add the following test function in the
testsdirectoryimport ballerina/io; import ballerina/test; @test:Config {} function test() { json res = clientEP->/users\-all(); io:println("obtained response: ", res); } -
Run
bal run- This is working -
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
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.
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.
@nipunayf Shall we check the generated jar and verify whether this field is there?
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
-
Generated client call in main.bal
-
Generated client call in test.bal
-
-
The client is in test.bal
- Generated client call in test.bal
- Generated client call in test.bal
@gabilang can you have a look on it?
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.