Description:
Function mocking does work when tests are in a package.
Steps to reproduce:
Have the below code in a package's test folder.
import ballerina/io;
import ballerina/test;
@test:Mock {
packageName: "notifications",
functionName: "intAdd"
}
public function mockIntAdd(int a, int b) returns int {
io:println("I'm the mock function!");
return (a - b);
}
@test:Config
function testAssertIntEquals() {
int answer = 0;
answer = intAdd(5, 3);
io:println("Function mocking test");
test:assertEquals(answer, 2, msg = "function mocking failed");
}
public function intAdd(int a, int b) returns int {
return (a + b);
}
Mocking wont work with ballerina test notifications/ or even ballerina test.
Affected Versions:
0.982.0 RC-1
OS, DB, other environment details and versions:
MAC, JDK-8
The issue here occurs because you haven't specified the full package path in the packageName attribute. I tried this out and giving the package name in the full form: pubudu/notifications:0.0.1 worked.
I think we need to decide on whether to make package version compulsory or not. In scenarios such as these, requiring the user to specify the package version doesn't make sense IMO since the function being mocked is in the same package.
Had no clue that it required the full package path. Then the variable should be packagePath instead of packageName. It should throw an error if such function was not found.
Hint: Use function pointer in the annotation to point to a function
This will be resolved when fixing #18780, hence closing this issue.
Most helpful comment
I think we need to decide on whether to make package version compulsory or not. In scenarios such as these, requiring the user to specify the package version doesn't make sense IMO since the function being mocked is in the same package.