SFDXHub
Browse/Apex Utility/T.apex (Test Utility)
🔧

T.apex (Test Utility)

Apex UtilityApexOpen Source
(0)

A test utility framework for Apex. Simplifies test data creation, assertions, and test setup with a fluent API designed to reduce test class boilerplate.

About

T.apex

T.apex is simply the Swiss knife for unit testing in Apex.

Why T.apex?

T.apex adopts expecting assertion style to make unit test codes more readable. Besides, T.apex provides powerful and easy mocking framework to make unit test easier. And T.apex has a built-in data generator to help you generate all kinds of random test data, including SObjects.

Dependencies

T.apex has a dependency on R.apex. Please include R.apex before getting started with T.apex.

Get Started

Expectations

T.expect(true).toBe(true);
T.expect(false).never.toBe(true);
T.expect(5).toEqual(5);
T.expect('abc').toMatch('.*b.*');
T.expect(null).toBeNull();
T.expect(true).toBeTrue();
T.expect(false).toBeFalse();
T.expect('abc').toContain('b');
T.expect(2).toBeLessThan(3);
T.expect(3).toBeGreaterThan(2);

Manual Failing

T.fail('Should fail here');

Create Mocks

Func mock = (Func)T.mock(Func.class);

Mock Method By Returning

T.when(mock.run(0)).thenReturn(0);
// When mock calls 'run' with 0, return 0

Mock Method By Throwing

T.when(mock.run(0)).thenThrow(new T.TestException('test'));
// When mock calls 'run' with 0, throw the exception

Mock Method By Answering

T.when(mock.run(0)).thenAnswer(R.inc);
// When mock calls 'run' with 0, apply the answer Func to the arguments
// and return the result

Argument Predicates

T.when(mock.run(T.anyBoolean(R.isNotNull))).thenReturn(0);
// When mock calls 'run' with any Boolean that is not null, return 0

Verify Mock Methods

mock.run(0);

T.verify(mock, 'run').toHaveBeenCalled();

Generate a Random Name

String name = (String)T.create('Name'); // Random person names

Generate a Random Sentence

String sentence = (String)T.create('Sentence'); // Random sentences

Generate a Random List of Strings

List<String> strList = (List<String>)T.create('List', new Map<String, Object>{
    'type' => 'String',
    'min' => 5
});
// Create a random list that contains at least 5 strings

Generate a Random SObject

Account acc = (Account)T.createSObject('Account', new Map<String, Object>{
    'fields' => new List<String>{ 'Description' }
});
// Create a random Account object that include 'Description' field

Reviews (0)

No reviews yet. Be the first to leave one!

Details

Author
Click-to-Cloud
Category
Apex Utility
Published
Feb 16, 2026