-
Notifications
You must be signed in to change notification settings - Fork 15
Adding CustomAssert class #344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
270 changes: 270 additions & 0 deletions
270
force-app/main/default/classes/test utilities/CustomAssert.cls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,270 @@ | ||
| /** | ||
| * @description This class contains custom assertion methods for use in tests. | ||
| */ | ||
| @IsTest | ||
| private class CustomAssert { | ||
| /** | ||
| * @description Assertion method to validate that two Datetime objects are equal within a given leeway | ||
| * | ||
| * @param expectedDatetime Datetime - the expected Datetime | ||
| * @param actualDatetime Datetime - the actual Datetime | ||
| * @param timeVarianceSeconds Integer - the number of seconds of leeway to allow | ||
| * @param msg String - the message to display if the assertion fails | ||
| */ | ||
| @SuppressWarnings('PMD.excessiveParameterList') | ||
| public static void areEqual( | ||
| Datetime expectedDatetime, | ||
| Datetime actualDatetime, | ||
| Integer timeVarianceSeconds, | ||
| String msg | ||
| ) { | ||
| Datetime expectedMinusVariance = expectedDatetime.addSeconds( | ||
| -timeVarianceSeconds | ||
| ); | ||
| Datetime expectedPlusVariance = expectedDatetime.addSeconds( | ||
| timeVarianceSeconds | ||
| ); | ||
| System.Assert.isTrue( | ||
| actualDatetime.getTime() >= expectedMinusVariance.getTime() && | ||
| actualDatetime.getTime() <= expectedPlusVariance.getTime(), | ||
| msg | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @description Assertion method to validate that a string starts with the given string | ||
| * @param str String - the string to validate | ||
| * @param prefix String - the prefix the string must start with | ||
| * @param msg String - the message to display if the assertion fails | ||
| */ | ||
| public static void startsWith(String str, String prefix, String msg) { | ||
| System.Assert.isTrue(str.startsWith(prefix), msg); | ||
| } | ||
|
|
||
| /** | ||
| * @description Assertion method to validate that a string starts with the given string (case insensitive) | ||
| * @param str String - the string to validate | ||
| * @param prefix String - the prefix the string must start with (case insensitive) | ||
| * @param msg String - the message to display if the assertion fails | ||
| */ | ||
| public static void startsWithIgnoreCase( | ||
| String str, | ||
| String prefix, | ||
| String msg | ||
| ) { | ||
| System.Assert.isTrue(str.startsWithIgnoreCase(prefix), msg); | ||
| } | ||
|
|
||
| /** | ||
| * @description Assertion method to validate that a string ends with the given string | ||
| * @param str String - the string to validate | ||
| * @param suffix String - the suffix the string must end with | ||
| * @param msg String - the message to display if the assertion fails | ||
| */ | ||
| public static void endsWith(String str, String suffix, String msg) { | ||
| System.Assert.isTrue(str.endsWith(suffix), msg); | ||
| } | ||
|
|
||
| /** | ||
| * @description Assertion method to validate that a string ends with the given string (case insensitive) | ||
| * @param str String - the string to validate (case insensitive) | ||
| * @param suffix String - the suffix the string must end with | ||
| * @param msg String - the message to display if the assertion fails | ||
| */ | ||
| public static void endsWithIgnoreCase(String str, String suffix, String msg) { | ||
| System.Assert.isTrue(str.endsWithIgnoreCase(suffix), msg); | ||
| } | ||
|
|
||
| /** | ||
| * @description An assertion that validates a list contains at least one of the items in the other list passed in | ||
| * @param listToCheck List<Object> the list to check | ||
| * @param listToCheckAgainst List<Object> the list to check against | ||
| * @param msg String the message to display if the assertion fails | ||
| */ | ||
| public static void contains( | ||
| List<Object> listToCheck, | ||
| List<Object> listToCheckAgainst, | ||
| String msg | ||
| ) { | ||
| contains( | ||
| new Set<Object>(listToCheck), | ||
| new Set<Object>(listToCheckAgainst), | ||
| msg | ||
| ); | ||
| System.Assert.fail(msg); | ||
| } | ||
|
|
||
| /** | ||
| * @description An assertion that validates the passed in list contains at least one of the items in the set. | ||
| * Ultimately defers to the contains assertion below comparing two sets. | ||
| * @param listToCheck List<Object> to check for containing at least one item from the set | ||
| * @param setToCheckAgainst Set<Object> the set of items to check for in the list | ||
| * @param msg String the message to display if the assertion fails | ||
| */ | ||
| public static void contains( | ||
| List<Object> listToCheck, | ||
| Set<Object> setToCheckAgainst, | ||
| String msg | ||
| ) { | ||
| Set<Object> listSet = new Set<Object>(listToCheck); | ||
| contains(setToCheckAgainst, listSet, msg); | ||
| System.Assert.fail(msg); | ||
| } | ||
|
Comment on lines
+96
to
+112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| /** | ||
| * @description Checks to see if a set contains any of the items in another set. | ||
| * @param setToCheck Set<Object> the set to check | ||
| * @param setToCheckAgainst Set<Object> the set to check against | ||
| * @param msg String the message to display if the assertion fails | ||
| */ | ||
| public static void contains( | ||
| Set<Object> setToCheck, | ||
| Set<Object> setToCheckAgainst, | ||
| String msg | ||
| ) { | ||
| for (Object item : setToCheck) { | ||
| if (setToCheckAgainst.contains(item)) { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| System.Assert.fail(msg); | ||
| } | ||
|
|
||
| /** | ||
| * @description Look, this is a quasi inside joke method. If you have to ask if it's janky, it is. For more | ||
| * information, ask @jeffKrantz over on SFDC Discord. | ||
| * @param msg String the message you want in your failure response | ||
| */ | ||
| public static void isNotJanky(String msg) { | ||
| System.Assert.fail(msg); | ||
| } | ||
|
|
||
| /** | ||
| * @description An assertion that validates a list is at least a certain size | ||
| * | ||
| * @param collection List<Object> - the list to validate | ||
| * @param minSize Integer - the minimum size the list must be | ||
| * @param msg String - the message to display if the assertion fails | ||
| */ | ||
| public static void listMeetsMinimumSize( | ||
| List<Object> collection, | ||
| Integer minSize, | ||
| String msg | ||
| ) { | ||
| System.Assert.isTrue(collection.size() >= minSize, msg); | ||
| } | ||
|
|
||
| /** | ||
| * @description An assertion that validates a string is deserializable to untyped JSON | ||
| * | ||
| * @param jsonString String - the string to validate | ||
| * @param msg String - the message to display if the assertion fails | ||
| */ | ||
| public static void stringIsDeserializableAsUntypedJson( | ||
| String jsonString, | ||
| String msg | ||
| ) { | ||
| System.Assert.isNotNull(JSON.deserializeUntyped(jsonString), msg); | ||
| } | ||
|
|
||
| /** | ||
| * @description An assertion to validate that the object is an SObject | ||
| * | ||
| * @param obj Object - the object to validate | ||
| * @param msg String - the message to display if the assertion fails | ||
| */ | ||
| public static void isSObject(Object obj, String msg) { | ||
| System.Assert.isInstanceOfType(obj, SObject.class, msg); | ||
| } | ||
|
|
||
| /** | ||
| * @description An assertion to validate that the exception is of the expected type | ||
| * | ||
| * @param incomingException Exception - the exception to validate | ||
| * @param expectedExceptionType Type - the expected type of the exception | ||
| * @param msg String - the message to display if the assertion fails | ||
| */ | ||
| public static void caughtExpectedException( | ||
| Exception incomingException, | ||
| Type expectedExceptionType, | ||
| String msg | ||
| ) { | ||
| System.Assert.isInstanceOfType( | ||
| incomingException, | ||
| expectedExceptionType, | ||
| msg | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @description An assertion to validate that the exception is of the expected type and contains the expected | ||
| * message | ||
| * | ||
| * @param incomingException Exception - the exception to validate | ||
| * @param expectedExceptionType Type - the expected type of the exception | ||
| * @param expectedExceptionMessage String - A string that must exist in the resulting exception message | ||
| * @param msg String - the message to display if the assertion fails | ||
| */ | ||
| @SuppressWarnings('PMD.ExcessiveParameterList') | ||
| public static void caughtExpectedException( | ||
| Exception incomingException, | ||
| Type expectedExceptionType, | ||
| String expectedExceptionMessage, | ||
| String msg | ||
| ) { | ||
| System.Assert.isInstanceOfType( | ||
| incomingException, | ||
| expectedExceptionType, | ||
| msg | ||
| ); | ||
| System.Assert.isTrue( | ||
| incomingException.getMessage() | ||
| .containsIgnoreCase(expectedExceptionMessage), | ||
| msg | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @description An assertion that validates the given value is within a given range | ||
| * | ||
| * @param value Integer - the value to validate | ||
| * @param min Integer - the minimum value the value must be | ||
| * @param max Integer - the maximum value the value must be | ||
| * @param msg String - the message to display if the assertion fails | ||
| */ | ||
| public static void isInRange( | ||
| Integer value, | ||
| Integer min, | ||
| Integer max, | ||
| String msg | ||
| ) { | ||
| System.Assert.isTrue(value >= min && value <= max, msg); | ||
| } | ||
|
|
||
| /** | ||
| * @description An Assertion that validates that LogEvent__c records were generated. | ||
| * Note: this relies on the Log class that's part of ApexKit | ||
| * | ||
| * @param msg String - the message to display if the assertion fails | ||
| */ | ||
| public static void logsWereGenerated(String msg) { | ||
| Integer logEvents = [ | ||
| SELECT COUNT() | ||
| FROM LogEvent__c | ||
| WITH SYSTEM_MODE | ||
| ]; | ||
| System.Assert.isTrue(logEvents > 0, msg); | ||
| } | ||
|
|
||
| /** | ||
| * @description An assertion that validates that LogEvent__c records were generated. | ||
| * Note this relies on the Log class that's part of ApexKit. This method override accepts no parameters | ||
| * but delegates to the variant above by specifying a default message. | ||
| */ | ||
| public static void logsWereGenerated() { | ||
| logsWereGenerated( | ||
| 'Though expected, no log events were generated - if youre not using test.StopTest the events will never publish' | ||
| ); | ||
| } | ||
| } | ||
5 changes: 5 additions & 0 deletions
5
force-app/main/default/classes/test utilities/CustomAssert.cls-meta.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
| <apiVersion>63.0</apiVersion> | ||
| <status>Active</status> | ||
| </ApexClass> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There seems to be an issue with the
containsmethod. TheSystem.Assert.fail(msg);on line 94 will always be executed, causing the test to fail every time this method is called, regardless of whether the condition in thecontainsmethod on line 89-93 is met or not. This line should be removed.- System.Assert.fail(msg);