T.apex is simply the Swiss knife for unit testing in 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.
T.apex has a dependency on R.apex. Please include R.apex before getting started with T.apex.
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);
T.fail('Should fail here');
Func mock = (Func)T.mock(Func.class);
T.when(mock.run(0)).thenReturn(0);
// When mock calls 'run' with 0, return 0
T.when(mock.run(0)).thenThrow(new T.TestException('test'));
// When mock calls 'run' with 0, throw the exception
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
T.when(mock.run(T.anyBoolean(R.isNotNull))).thenReturn(0);
// When mock calls 'run' with any Boolean that is not null, return 0
mock.run(0);
T.verify(mock, 'run').toHaveBeenCalled();
String name = (String)T.create('Name'); // Random person names
String sentence = (String)T.create('Sentence'); // Random sentences
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
Account acc = (Account)T.createSObject('Account', new Map<String, Object>{
'fields' => new List<String>{ 'Description' }
});
// Create a random Account object that include 'Description' field