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 AssertError with the custom message and category.

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

function assertEquals(anydata | error actual, anydata expected, string msg)
Isolated Function

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 }

Parameters

  • actual anydata | error
  • Actual value

  • expected anydata
  • Expected value

  • msg string (default "Assertion Failed!")
  • Assertion error message

assertExactEquals

function assertExactEquals(any | error actual, any | error expected, string msg)
Isolated Function

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 }

Parameters

  • actual any | error
  • Actual value

  • expected any | error
  • Expected value

  • msg string (default "Assertion Failed!")
  • Assertion error message

assertFail

function assertFail(string msg)
Isolated Function

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 panic
5 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

assertFalse

function assertFalse(boolean condition, string msg)
Isolated Function

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 }

Parameters

  • condition boolean
  • Boolean condition to evaluate

  • msg string (default "Assertion Failed!")
  • Assertion error message

assertNotEquals

function assertNotEquals(anydata actual, anydata expected, string msg)
Isolated Function

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 anydata
  • Actual value

  • expected anydata
  • Expected value

  • msg string (default "Assertion Failed!")
  • Assertion error message

assertNotExactEquals

function assertNotExactEquals(any | error actual, any | error expected, string msg)
Isolated Function

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 }

Parameters

  • actual any | error
  • Actual value

  • expected any | error
  • Expected value

  • msg string (default "Assertion Failed!")
  • Assertion error message

assertTrue

function assertTrue(boolean condition, string msg)
Isolated Function

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 }

Parameters

  • condition boolean
  • Boolean condition to evaluate

  • msg string (default "Assertion Failed!")
  • Assertion error message

createBallerinaError

function createBallerinaError(string errorMessage, string category) returns error
Isolated Function

Creates an AssertError with the custom message and category.

Parameters

  • errorMessage string
  • Custom message for the Ballerina error

  • category string
  • Error category

Return Type

(error)

An AssertError with custom message and category

mock

function mock(typedesc<object{}> T, object{}  mockObject) returns T
Isolated Function

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
Isolated Function

Prepares a provided default mock object for stubbing.

Parameters

  • mockObject object {}
  • Created default mock object

Return Type

(MockObject)

Prepared object that allows a member function/field to register stubs

when

function when(MockFunction mockFunction) returns FunctionStub
Isolated Function

Objects and functions related to function mocking Allows a function to stub.

Parameters

Return Type

(FunctionStub)

Object that allows stubbing calls to provided function