Behavior Driven Development(BDD) Style Assertion
T.apex favors a BDD-style assertion like this:
T.expect(result).toBe(true);
T.apex favors a BDD-style assertion like this:
T.expect(result).toBe(true);
Here is a list of matchers used in T.apex.
Method | Description |
---|---|
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 |
Here is how we negate the matchers.
T.expect(result).never.toBe(null);
Here is how we manually fail the test.
T.fail('Should fail here');
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.
Method | Description |
---|---|
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 |
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.