T.apex

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

Argument Predicates Guide

Argument Predicates

What are Argument Predicates?

Argument predicates are used during establishing method behavior to capture method invocations more precisely.

For example,

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

This mock method of 'run' will only get triggered when a Boolean that is not null is passed in.

How to Use Argument Predicates

We can choose appropriate argument predicates according to the parameter type and our needs.

Let's say we want to use argument predicates in this method.

mock.setItem(Integer, String)

We can establish the method behavior without using argument predicates like this:

T.when(mock.setItem(0, 'a')).thenReturn(null);

Or we can introduce one at the first parameter.

T.when(mock.setItem(T.anyInteger(0), T.anyString('a'))).thenReturn(null);

Note that if we use argument predicates in any of the parameters, we need to apply them to all of the parameters.

Argument Predicates Usage

We can use argument predicates to represent any object of a specific type, like:

T.when(mock.run(T.anyBoolean())).thenReturn(0);

This gets triggered whenever a Boolean is passed in.

Or we can use argument predicates to represent an object with the specific value, like:

T.when(mock.run(T.anyBoolean(true))).thenReturn(0);

This gets triggered only when true is passed in.

Or we can use argument predicates to check according to some Funcs, like:

T.when(mock.run(T.anyBoolean(R.isNotNull))).thenReturn(0);

This gets triggered only when a not-null Boolean is passed in.

Argument Predicate Types

Here is a list of the argument predicate types that we can use in T.apex.

MethodDescription
any(...)Matches any Object
anyBoolean(...)Matches any Boolean
anyInteger(...)Matches any Integer
anyLong(...)Matches any Long
anyDouble(...)Matches any Double
anyDecimal(...)Matches any Decimal
anyString(...)Matches any String
anyList(...)Matches any List
anySet(...)Matches any Set
anyMap(...)Matches any Map
anySObject(...)Matches any SObject
anyDate(...)Matches any Date
anyTime(...)Matches any Time
anyDatetime(...)Matches any Datetime

Contribute on Github! Edit this section.