Skip to content

Commit 1e34e3f

Browse files
authored
Adding CustomAssert class (#344)
1 parent 6fb568c commit 1e34e3f

2 files changed

Lines changed: 275 additions & 0 deletions

File tree

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
/**
2+
* @description This class contains custom assertion methods for use in tests.
3+
*/
4+
@IsTest
5+
private class CustomAssert {
6+
/**
7+
* @description Assertion method to validate that two Datetime objects are equal within a given leeway
8+
*
9+
* @param expectedDatetime Datetime - the expected Datetime
10+
* @param actualDatetime Datetime - the actual Datetime
11+
* @param timeVarianceSeconds Integer - the number of seconds of leeway to allow
12+
* @param msg String - the message to display if the assertion fails
13+
*/
14+
@SuppressWarnings('PMD.excessiveParameterList')
15+
public static void areEqual(
16+
Datetime expectedDatetime,
17+
Datetime actualDatetime,
18+
Integer timeVarianceSeconds,
19+
String msg
20+
) {
21+
Datetime expectedMinusVariance = expectedDatetime.addSeconds(
22+
-timeVarianceSeconds
23+
);
24+
Datetime expectedPlusVariance = expectedDatetime.addSeconds(
25+
timeVarianceSeconds
26+
);
27+
System.Assert.isTrue(
28+
actualDatetime.getTime() >= expectedMinusVariance.getTime() &&
29+
actualDatetime.getTime() <= expectedPlusVariance.getTime(),
30+
msg
31+
);
32+
}
33+
34+
/**
35+
* @description Assertion method to validate that a string starts with the given string
36+
* @param str String - the string to validate
37+
* @param prefix String - the prefix the string must start with
38+
* @param msg String - the message to display if the assertion fails
39+
*/
40+
public static void startsWith(String str, String prefix, String msg) {
41+
System.Assert.isTrue(str.startsWith(prefix), msg);
42+
}
43+
44+
/**
45+
* @description Assertion method to validate that a string starts with the given string (case insensitive)
46+
* @param str String - the string to validate
47+
* @param prefix String - the prefix the string must start with (case insensitive)
48+
* @param msg String - the message to display if the assertion fails
49+
*/
50+
public static void startsWithIgnoreCase(
51+
String str,
52+
String prefix,
53+
String msg
54+
) {
55+
System.Assert.isTrue(str.startsWithIgnoreCase(prefix), msg);
56+
}
57+
58+
/**
59+
* @description Assertion method to validate that a string ends with the given string
60+
* @param str String - the string to validate
61+
* @param suffix String - the suffix the string must end with
62+
* @param msg String - the message to display if the assertion fails
63+
*/
64+
public static void endsWith(String str, String suffix, String msg) {
65+
System.Assert.isTrue(str.endsWith(suffix), msg);
66+
}
67+
68+
/**
69+
* @description Assertion method to validate that a string ends with the given string (case insensitive)
70+
* @param str String - the string to validate (case insensitive)
71+
* @param suffix String - the suffix the string must end with
72+
* @param msg String - the message to display if the assertion fails
73+
*/
74+
public static void endsWithIgnoreCase(String str, String suffix, String msg) {
75+
System.Assert.isTrue(str.endsWithIgnoreCase(suffix), msg);
76+
}
77+
78+
/**
79+
* @description An assertion that validates a list contains at least one of the items in the other list passed in
80+
* @param listToCheck List<Object> the list to check
81+
* @param listToCheckAgainst List<Object> the list to check against
82+
* @param msg String the message to display if the assertion fails
83+
*/
84+
public static void contains(
85+
List<Object> listToCheck,
86+
List<Object> listToCheckAgainst,
87+
String msg
88+
) {
89+
contains(
90+
new Set<Object>(listToCheck),
91+
new Set<Object>(listToCheckAgainst),
92+
msg
93+
);
94+
System.Assert.fail(msg);
95+
}
96+
97+
/**
98+
* @description An assertion that validates the passed in list contains at least one of the items in the set.
99+
* Ultimately defers to the contains assertion below comparing two sets.
100+
* @param listToCheck List<Object> to check for containing at least one item from the set
101+
* @param setToCheckAgainst Set<Object> the set of items to check for in the list
102+
* @param msg String the message to display if the assertion fails
103+
*/
104+
public static void contains(
105+
List<Object> listToCheck,
106+
Set<Object> setToCheckAgainst,
107+
String msg
108+
) {
109+
Set<Object> listSet = new Set<Object>(listToCheck);
110+
contains(setToCheckAgainst, listSet, msg);
111+
System.Assert.fail(msg);
112+
}
113+
114+
/**
115+
* @description Checks to see if a set contains any of the items in another set.
116+
* @param setToCheck Set<Object> the set to check
117+
* @param setToCheckAgainst Set<Object> the set to check against
118+
* @param msg String the message to display if the assertion fails
119+
*/
120+
public static void contains(
121+
Set<Object> setToCheck,
122+
Set<Object> setToCheckAgainst,
123+
String msg
124+
) {
125+
for (Object item : setToCheck) {
126+
if (setToCheckAgainst.contains(item)) {
127+
return;
128+
}
129+
}
130+
131+
System.Assert.fail(msg);
132+
}
133+
134+
/**
135+
* @description Look, this is a quasi inside joke method. If you have to ask if it's janky, it is. For more
136+
* information, ask @jeffKrantz over on SFDC Discord.
137+
* @param msg String the message you want in your failure response
138+
*/
139+
public static void isNotJanky(String msg) {
140+
System.Assert.fail(msg);
141+
}
142+
143+
/**
144+
* @description An assertion that validates a list is at least a certain size
145+
*
146+
* @param collection List<Object> - the list to validate
147+
* @param minSize Integer - the minimum size the list must be
148+
* @param msg String - the message to display if the assertion fails
149+
*/
150+
public static void listMeetsMinimumSize(
151+
List<Object> collection,
152+
Integer minSize,
153+
String msg
154+
) {
155+
System.Assert.isTrue(collection.size() >= minSize, msg);
156+
}
157+
158+
/**
159+
* @description An assertion that validates a string is deserializable to untyped JSON
160+
*
161+
* @param jsonString String - the string to validate
162+
* @param msg String - the message to display if the assertion fails
163+
*/
164+
public static void stringIsDeserializableAsUntypedJson(
165+
String jsonString,
166+
String msg
167+
) {
168+
System.Assert.isNotNull(JSON.deserializeUntyped(jsonString), msg);
169+
}
170+
171+
/**
172+
* @description An assertion to validate that the object is an SObject
173+
*
174+
* @param obj Object - the object to validate
175+
* @param msg String - the message to display if the assertion fails
176+
*/
177+
public static void isSObject(Object obj, String msg) {
178+
System.Assert.isInstanceOfType(obj, SObject.class, msg);
179+
}
180+
181+
/**
182+
* @description An assertion to validate that the exception is of the expected type
183+
*
184+
* @param incomingException Exception - the exception to validate
185+
* @param expectedExceptionType Type - the expected type of the exception
186+
* @param msg String - the message to display if the assertion fails
187+
*/
188+
public static void caughtExpectedException(
189+
Exception incomingException,
190+
Type expectedExceptionType,
191+
String msg
192+
) {
193+
System.Assert.isInstanceOfType(
194+
incomingException,
195+
expectedExceptionType,
196+
msg
197+
);
198+
}
199+
200+
/**
201+
* @description An assertion to validate that the exception is of the expected type and contains the expected
202+
* message
203+
*
204+
* @param incomingException Exception - the exception to validate
205+
* @param expectedExceptionType Type - the expected type of the exception
206+
* @param expectedExceptionMessage String - A string that must exist in the resulting exception message
207+
* @param msg String - the message to display if the assertion fails
208+
*/
209+
@SuppressWarnings('PMD.ExcessiveParameterList')
210+
public static void caughtExpectedException(
211+
Exception incomingException,
212+
Type expectedExceptionType,
213+
String expectedExceptionMessage,
214+
String msg
215+
) {
216+
System.Assert.isInstanceOfType(
217+
incomingException,
218+
expectedExceptionType,
219+
msg
220+
);
221+
System.Assert.isTrue(
222+
incomingException.getMessage()
223+
.containsIgnoreCase(expectedExceptionMessage),
224+
msg
225+
);
226+
}
227+
228+
/**
229+
* @description An assertion that validates the given value is within a given range
230+
*
231+
* @param value Integer - the value to validate
232+
* @param min Integer - the minimum value the value must be
233+
* @param max Integer - the maximum value the value must be
234+
* @param msg String - the message to display if the assertion fails
235+
*/
236+
public static void isInRange(
237+
Integer value,
238+
Integer min,
239+
Integer max,
240+
String msg
241+
) {
242+
System.Assert.isTrue(value >= min && value <= max, msg);
243+
}
244+
245+
/**
246+
* @description An Assertion that validates that LogEvent__c records were generated.
247+
* Note: this relies on the Log class that's part of ApexKit
248+
*
249+
* @param msg String - the message to display if the assertion fails
250+
*/
251+
public static void logsWereGenerated(String msg) {
252+
Integer logEvents = [
253+
SELECT COUNT()
254+
FROM LogEvent__c
255+
WITH SYSTEM_MODE
256+
];
257+
System.Assert.isTrue(logEvents > 0, msg);
258+
}
259+
260+
/**
261+
* @description An assertion that validates that LogEvent__c records were generated.
262+
* Note this relies on the Log class that's part of ApexKit. This method override accepts no parameters
263+
* but delegates to the variant above by specifying a default message.
264+
*/
265+
public static void logsWereGenerated() {
266+
logsWereGenerated(
267+
'Though expected, no log events were generated - if youre not using test.StopTest the events will never publish'
268+
);
269+
}
270+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>63.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>

0 commit comments

Comments
 (0)