Functions
assertEquals | I Asserts whether the given values are equal. |
assertExactEquals | I Asserts whether the given values are exactly equal. |
assertFail | I Assert failure is triggered based on your discretion. |
assertFalse | I Asserts whether the given condition is false. |
assertNotEquals | I Asserts whether the given values are not equal. |
assertNotExactEquals | I Asserts whether the given values are not exactly equal. |
assertTrue | I Asserts whether the given condition is true. |
createBallerinaError | I Creates an |
mock | I Creates a mock object of provided type description. |
prepare | I Prepares a provided default mock object for stubbing. |
when | I Objects and functions related to function mocking Allows a function to stub. |
assertEquals
Asserts whether the given values are equal. If it is not, an AssertError is thrown with the given errorMessage.
Example
1import ballerina/test;2@test:Config {}3 function testAssertIntEquals() {4 int answer = intAdd(5, 3);5 test:assertEquals(answer, 8, msg = "IntAdd function failed");6 }7 function intAdd(int a, int b) returns (int) {8 return (a + b);9 }
assertExactEquals
Asserts whether the given values are exactly equal. If it is not, an AssertError is thrown with the given errorMessage.
Example
1import ballerina/test;2 class Person {3 public string name = "";4 public int age = 0;5 public Person? parent = ();6 private string email = "default@abc.com";7 string address = "No 20, Palm grove";8 }9 @test:Config {}10 function testAssertExactEqualsObject() {11 Person p1 = new;12 Person p2 = p1;13 test:assertExactEquals(p1, p2, msg = "Objects are not exactly equal");14 }
assertFail
function assertFail(string msg) returns never
Assert failure is triggered based on your discretion. AssertError is thrown with the given errorMessage.
Example
1import ballerina/test;2 @test:Config {}3 function foo() {4 error? e = trap bar(); // Expecting `bar()` to panic5 if (e is error) {6 test:assertEquals(e.message().toString(), "Invalid Operation", msg = "Invalid error reason");7 } else {8 test:assertFail(msg = "Expected an error");9 }10 }11 function bar() {12 panic error("Invalid Operation");13 }
Parameters
- msg string (default "Test Failed!")
Assertion error message
Return Type
(never)never returns a value
assertFalse
Asserts whether the given condition is false. If it is not, a AssertError is thrown with the given errorMessage.
Example
1import ballerina/test;2 @test:Config {}3 function testAssertFalse() {4 boolean value = false;5 test:assertFalse(value, msg = "AssertFalse failed");6 }
assertNotEquals
function assertNotEquals(any actual, anydata expected, string msg)
Asserts whether the given values are not equal. If it is equal, an AssertError is thrown with the given errorMessage.
Example
1import ballerina/test;2 @test:Config {}3 function testAssertIntEquals() {4 int answer = intAdd(5, 3);5 test:assertNotEquals(answer, 8, msg = "Matches");6 }7 function intAdd(int a, int b) returns (int) {8 return (a + b);9 }
Parameters
- actual any
Actual value
- expected anydata
Expected value
- msg string (default "Assertion Failed!")
Assertion error message
assertNotExactEquals
Asserts whether the given values are not exactly equal. If it is equal, an AssertError is thrown with the given errorMessage.
Example
1import ballerina/test;2class Person {3 public string name = "";4 public int age = 0;5 public Person? parent = ();6 private string email = "default@abc.com";7 string address = "No 20, Palm grove";8 }9 @test:Config {}10 function testAssertNotExactEqualsObject() {11 Person p1 = new;12 Person p2 = new ();13 test:assertNotExactEquals(p1, p2, msg = "Objects are exactly equal");14 }
assertTrue
Asserts whether the given condition is true. If it is not, a AssertError is thrown with the given errorMessage.
Example
1import ballerina/test;2 @test:Config {}3 function testAssertTrue() {4 boolean value = false;5 test:assertTrue(value, msg = "AssertTrue failed");6 }
createBallerinaError
Creates an AssertError
with the custom message and category.
mock
function mock(typedesc<object{}> T, object{} mockObject) returns T
Creates a mock object of provided type description.
Parameters
- T typedesc<object{}>
Type of object to create the mock
- mockObject object{} (default object { })
Mock object to replace the original (optional)
Return Type
(T)Created mock object or throw an error if validation failed
prepare
function prepare(object {} mockObject) returns MockObject
Prepares a provided default mock object for stubbing.
Parameters
- mockObject object {}
Created default mock object
when
function when(MockFunction mockFunction) returns FunctionStub
Objects and functions related to function mocking Allows a function to stub.
Parameters
- mockFunction MockFunction
Function name to allow stubbing