Hi @unkempt loom
You can use object mocking to write tests for this. Following is a sample demonstrating this.
main.bal
import ballerina/io;
public class Operation {
public function intAdd(int a, int b) returns int {
return a+b;
}
}
Operation op = check new Operation();
public function getAdd() returns int {
io:println(op.intAdd(3, 4));
return op.intAdd(3, 4); // returns 7
}
main_test.bal
import ballerina/test;
import ballerina/io;
public class MockOperation {
public function intAdd(int a, int b) returns int {
return 5;
}
}
@test:Config
public function testIntAdd() {
op = test:mock(Operation, new MockOperation());
int result = getAdd(); // returns 5
io:print(result);
// verify that the function returns the mock value after replacing the name
test:assertEquals(result, 5);
}
In the above sample, when we run the testIntAdd test, it will use the MockOperation class in place of the actual Operation class and the getAdd method will call the intAdd function in the MockOperation class instead of the actual intAdd method.
You can refer the below documentations for further insight on this.
[1] https://ballerina.io/learn/test-ballerina-code/mocking/
[2] https://ballerina.io/learn/by-example/testerina-mocking-objects/
Hope this helps with your problem