T.apex

  • Docs
  • Tutorials
Docs Menu
  • BDD Style
  • Mocking
    • Mock Creation
    • Mock Behavior
    • Argument Predicates
    • Mock Verification
  • Test Data

Test Data Guide

Generate random test data

Random Data Generator

A full-featured random data generator is shipped with T.apex. We can use this random data generator to generate test data.

In test code, we can do this:

String s = (String)T.create('String');

And this will generate a random string for us.

Also we can use below, which is equivalent to the above.

String s = (String)Random.anyString.run();

Random is just the random data generator in T.apex. Note that T can only be used in test codes, while Randomis just a utility class and can be used anywhere outside test codes.

Therefore, if we want to generate random data outside test codes, please use the latter form.

Generator Options

We can actually pass in options to fine tune how we would like the random data to be generated.

String s = (String)T.create('String', new Map{ ... });

Or

String s = (String)Random.anyString.run(new Map{ ... });

are the equivalent. The details of the options rely on the random data we want to generate.

Random Object

We can create a random Object like this:

Random.anyObject.run(new Map{
    'type' => 'String',
    'config' => new Map{ 'min' => 5 }
})

Here are the options:

KeyValue TypeDescription
typeStringThe type of data to create
configMapThe options to create this type of data

Available types are:

NameDescription
BooleanBoolean
IntegerInteger
LongLong
DoubleDouble
DecimalDecimal
CharCharacter
StringString
ListList
SetSet
MapMap
SObjectSObject
DateDate
TimeTime
DatetimeDatetime
SyllableA syllable in the word
WordWord
SentenceSentence
ParagraphParagraph
GenderGender
FirstnameFirstname
LastnameLastname
PrefixPrefix
SuffixSuffix
NamePerson name
EmailEmail
DomainDomain

For any other types, T.apex will try to find the Type and instantiate it.

Random Boolean

We can create a random Boolean like this:

Random.anyBoolean.run(new Map{
    'likelihood' => 60
})

Here are the options:

KeyValue TypeDescription
likelihoodIntegerThe likelihood of being true, default to 50

Random Integer

We can create a random Integer like this:

Random.anyInteger.run(new Map{
    'min' => 5,
    'max' => 20
})

Here are the options:

KeyValue TypeDescription
minIntegerThe min value, default to Random.MIN_INTEGER
maxIntegerThe max value, default to Random.MAX_INTEGER

Random Long

We can create a random Long like this:

Random.anyLong.run(new Map{
    'min' => 5,
    'max' => 20
})

Here are the options:

KeyValue TypeDescription
minLongThe min value, default to Random.MIN_LONG
maxLongThe max value, default to Random.MAX_LONG

Random Double

We can create a random Double like this:

Random.anyDouble.run(new Map{
    'min' => 5,
    'max' => 20
})

Here are the options:

KeyValue TypeDescription
minDoubleThe min value, default to Random.MIN_DOUBLE
maxDoubleThe max value, default to Random.MAX_DOUBLE

Random Decimal

We can create a random Decimal like this:

Random.anyDecimal.run(new Map{
    'min' => 5,
    'max' => 20
})

Here are the options:

KeyValue TypeDescription
minDecimalThe min value, default to Random.MIN_DECIMAL
maxDecimalThe max value, default to Random.MAX_DECIMAL

Random Char

We can create a random Char like this:

Random.anyChar.run(new Map{
    'alpha' => true
})

Here are the options:

KeyValue TypeDescription
casingStringupper/lower/null, default to all
poolStringThe pool to get the character from, default to null
alphaBooleanWhether to pick only alphabetic, default to false
symbolsBooleanWhether to pick only symbols, default to false
numbersBooleanWhether to pick only numbers, default to false

Random String

We can create a random String like this:

Random.anyList.run(new Map{
    'type' => 'String',
    'min' => 5
})

Here are the options:

KeyValue TypeDescription
minIntegerThe min length, default to 0
maxIntegerThe max length, default to 20
**Passed down to Char

Random List

We can create a random List like this:

Random.anyList.run(new Map{
    'type' => 'String',
    'min' => 5
})

Here are the options:

KeyValue TypeDescription
minIntegerThe min length, default to 0
maxIntegerThe max length, default to 20
poolListThe pool to draw from, default to null
typeStringThe type of elements in the list, default to null
configMapThe config of the elements to be created, default to null

pool is used as a group while type and config are used as a group.

Random Set

We can create a random Set like this:

Random.anySet.run(new Map{
    'min' => 5
})

Here are the options:

KeyValue TypeDescription
minIntegerThe min length, default to 0
maxIntegerThe max length, default to 20
poolSetThe pool to draw from, default to null
typeStringType is fixed to 'String' and can be omitted, default to null
configMapThe config of the elements to be created, default to null

pool is used as a group while type and config are used as a group.

Random Map

We can create a random Map like this:

Random.anyMap.run(new Map{
    'type' => 'String',
    'min' => 5
})

Here are the options:

KeyValue TypeDescription
minIntegerThe min length, default to 0
maxIntegerThe max length, default to 20
poolMapThe pool to draw from, default to null
typeStringType is fixed to 'String' and can be omitted, default to null
configMapThe config of the elements to be created, default to null

pool is used as a group while type and config are used as a group.

Random Date

We can create a random Date like this:

Random.anyDate.run(new Map{
    'min' => Datetime.now().getTime(),
    'max' => Datetime.now().addYears(1).getTime()
})

Here are the options:

KeyValue TypeDescription
minLongThe start datetime, default to now
maxLongThe end datetime, default to 20 years later

Random Time

We can create a random Time like this:

Random.anyTime.run(new Map{
    'min' => Datetime.now().getTime(),
    'max' => Datetime.now().addYears(1).getTime()
})

Here are the options:

KeyValue TypeDescription
minLongThe start datetime, default to now
maxLongThe end datetime, default to 20 years later

Random Datetime

We can create a random Datetime like this:

Random.anyDatetime.run(new Map{
    'min' => Datetime.now().getTime(),
    'max' => Datetime.now().addYears(1).getTime()
})

Here are the options:

KeyValue TypeDescription
minLongThe start datetime, default to now
maxLongThe end datetime, default to 20 years later

Random Syllable

We can create a random Syllable like this:

Random.anySyllable.run(new Map{
    'length' => 4
})

Here are the options:

KeyValue TypeDescription
lengthIntegerThe length of the syllable, default to 2 or 3
capitalizeBooleanWhether to capitalize the word, default to false

Random Word

We can create a random Word like this:

Random.anyWord.run(new Map{
    'syllables' => 4
})

Here are the options:

KeyValue TypeDescription
lengthIntegerThe length of the word, default to null
capitalizeBooleanWhether to capitalize the word, default to false
syllablesIntegerThe number of syllables, default to 1-3

length and syllables are used separately.

Random Sentence

We can create a random Sentence like this:

Random.anySentence.run(new Map{
    'words' => 20
})

Here are the options:

KeyValue TypeDescription
wordsIntegerThe number of words, default to 12-18
punctuationStringThe punctuation to append to the sentence, default to '.'

Random Paragraph

We can create a random Paragraph like this:

Random.anyParagraph.run(new Map{
    'sentences' => 5
})

Here are the options:

KeyValue TypeDescription
sentencesIntegerThe number of sentences, default to 3-7

Random Gender

We can create a random Gender like this:

Random.anyGender.run(new Map{
    'extra' => new List{ 'Unisex' }
})

Here are the options:

KeyValue TypeDescription
extraListThe extra list of genders, default to empty list

Random Firstname

We can create a random Firstname like this:

Random.anyFirstname.run()

Here are the options:

KeyValue TypeDescription
genderStringMale/Female/null, the gender according to which to pick first names, default to null, which means no specific gender

Random Lastname

We can create a random Lastname like this:

Random.anyLastname.run()

Here are the options:

KeyValue TypeDescription

Random Prefix

We can create a random Prefix like this:

Random.anyPrefix.run()

Here are the options:

KeyValue TypeDescription
genderStringMale/Female/all/null, the gender according to which to pick prefixes, default to null, which means all prefixes

Random Suffix

We can create a random Suffix like this:

Random.anySuffix.run()

Here are the options:

KeyValue TypeDescription

Random Name

We can create a random Name like this:

Random.anyName.run()

Here are the options:

KeyValue TypeDescription
middleBooleanWhether to generate middle names, default to false
middleInitialBooleanWhether to generate middle initial names, default to false
prefixBooleanWhether to generate prefixes, default to false
suffixBooleanWhether to generate suffixes, default to false

Random Email

We can create a random Email like this:

Random.anyEmail.run()

Here are the options:

KeyValue TypeDescription
lengthIntegerThe length of the word before '@', default to null
domainStringThe domain of the email, default to be random domain

Random Domain

We can create a random Domain like this:

Random.anyDomain.run()

Here are the options:

KeyValue TypeDescription

Random SObject

We can create a random SObject like this:

Random.anySObject.run(new Map{
    'type' => 'Account'
})

Here are the options:

KeyValue TypeDescription
typeStringThe type of the SObject, required
cascadeBooleanWhether to generate reference field with SObjects, default to false
fieldsList or MapSpecify which fields to generate

When fields is of type List, all specified fields in the list will be filled in the generated SObject.

When fields is of type Map, according to the value mapped by the field keys, if it is a

  • non-Func value, filling the generated SObject field with the value

  • Func value, filling the generated SObject field with the value computed using the Func

You can add included fields or excluded fields.

Random.addIncludedFields('Account', new Set{ 'Description' });

This will impact all the data generation afterwards, adding the 'Description' field when creating Account objects.

Contribute on Github! Edit this section.