import ballerina/test;
import ballerina/io;
@test:Mock {
packageName : "." ,
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);
}
Testerina Function MocksMock functions allow you to hide the real function and engage your own functions when running tests. This allows you to isolate your test functions from the rest. |
|
import ballerina/test;
import ballerina/io;
|
|
@test:Mock {
|
THis is the mock function which will replace the real function |
packageName : "." ,
functionName : "intAdd"
}
public function mockIntAdd (int a, int b) returns (int) {
io:println("I'm the mock function!");
return (a-b);
}
|
Since we don’t have a package declaration, . is the current package |
@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);
}
|
|
$ ballerina test testerina-function-mocks.bal
---------------------------------------------------------------------------
T E S T S
---------------------------------------------------------------------------
---------------------------------------------------------------------------
Running Tests of Package: .
---------------------------------------------------------------------------
I'm the mock function!
Function mocking test
|
|
Tests run: 1, Passed: 1, Failures: 0, Skipped: 0 - in TestSuite
|
|
---------------------------------------------------------------------------
Results:
|
|
Tests run: 1, Passed: 1, Failures: 0, Skipped: 0
---------------------------------------------------------------------------
Summary:
|
|
................................................................... SUCCESS
---------------------------------------------------------------------------
|
|