T.apex

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

BDD Style Guide

BDD Style Assertion

Behavior Driven Development(BDD) Style Assertion

T.apex favors a BDD-style assertion like this:

T.expect(result).toBe(true);

Matchers

Here is a list of matchers used in T.apex.

MethodDescription
toBe(Object)Check equality
toEqual(Object)Check equality
toMatch(String)Check string pattern matching
toBeNull()Check null
toBeTrue()Check True
toBeFalse()Check False
toContain(Object)Check containing
toBeLessThan(Object)Check less than
toBeGreaterThan(Object)Check greater than

Negate Matchers

Here is how we negate the matchers.

T.expect(result).never.toBe(null);

Manual Failing

Here is how we manually fail the test.

T.fail('Should fail here');

Asymmetric Matchers

Asymmetric matchers are a group of matchers that implements T.IMatcher, which usually contains asymmetric equality checking logic.

Here is how we use the asymmetric matchers.

T.expect(result).toEqual(T.objectOfAny(String.class));

Here is a list of the asymmetric matchers provided by T.apex.

MethodDescription
objectOfAny(Type)Matches any object of given Type
objectOfAnything()Matches anything but null
mapContaining(Map)Contains all the key-value pairs
listContaining(List)Contains all elements from the list

Custom Asymmetric Matchers

We can also implement our custom asymmetric matchers.

public class CustomMatcher implements IMatcher {
    private String word;

    public CustomMatcher(String word) {
        this.word = word;
    }

    public String getMessage(Object other) {
        return 'Custom matcher fails';
    }

    public Boolean matches(Object other) {
        return this.word == other;
    }
}

T.expect(result).toEqual(new CustomMatcher('word'));

Contribute on Github! Edit this section.