forked from apex-enterprise-patterns/fflib-apex-common
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test for ServiceUtils.logAndRethrow
- Loading branch information
1 parent
5ae972a
commit c7a6997
Showing
3 changed files
with
104 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
81 changes: 81 additions & 0 deletions
81
framework/default/ortoo-core/default/classes/utils/tests/ServiceUtilsTest.cls
This file contains 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,81 @@ | ||
@isTest | ||
private without sharing class ServiceUtilsTest | ||
{ | ||
@isTest | ||
private static void logAndRethrow_whenGivenAStandardException_logsItAndRethrowsIt() // NOPMD: Test method name format | ||
{ | ||
TestLoggerUtils.switchLoggingOn(); | ||
TestLoggerService logger = TestLoggerUtils.registerTestLoggerService(); | ||
|
||
DmlException exceptionToLog = new DmlException( 'The exception to log' ); | ||
Exception exceptionCaught; | ||
|
||
Test.startTest(); | ||
try | ||
{ | ||
ServiceUtils.logAndRethrow( exceptionToLog ); | ||
} | ||
catch( Exception e ) | ||
{ | ||
exceptionCaught = e; | ||
} | ||
Test.stopTest(); | ||
|
||
logger.assertNumberOfLogCalls( 1, 'logAndRethrow, when given an exception, will log it with the logger service' ); | ||
System.assertEquals( exceptionCaught, exceptionToLog, 'logAndRethrow, when given an exception, will rethrow it' ); | ||
} | ||
|
||
@isTest | ||
private static void logAndRethrow_whenGivenAnOrtooException_logsItStripsItAndRethrowsIt() // NOPMD: Test method name format | ||
{ | ||
TestLoggerUtils.switchLoggingOn(); | ||
TestLoggerService logger = TestLoggerUtils.registerTestLoggerService(); | ||
|
||
Amoss_Instance exceptionController = new Amoss_Instance( ortoo_Exception.class ); | ||
exceptionController | ||
.expects( 'stripContexts' ); | ||
|
||
Exception exceptionToLog = (Exception)exceptionController.getDouble(); | ||
Exception exceptionCaught; | ||
|
||
Test.startTest(); | ||
try | ||
{ | ||
ServiceUtils.logAndRethrow( exceptionToLog ); | ||
} | ||
catch( Exception e ) | ||
{ | ||
exceptionCaught = e; | ||
} | ||
Test.stopTest(); | ||
|
||
logger.assertNumberOfLogCalls( 1, 'logAndRethrow, when given an ortoo exception, will log it with the logger service' ); | ||
System.assertEquals( String.valueOf( exceptionCaught ), String.valueOf( exceptionToLog ), 'logAndRethrow, when given an ortoo exception, will rethrow it' ); | ||
|
||
exceptionController.verify(); | ||
} | ||
|
||
@isTest | ||
private static void logAndRethrow_whenGivenAValidationException_willNotLogIt() // NOPMD: Test method name format | ||
{ | ||
TestLoggerUtils.switchLoggingOn(); | ||
TestLoggerService logger = TestLoggerUtils.registerTestLoggerService(); | ||
|
||
Exception exceptionToLog = new Exceptions.ValidationException( 'ValidationFailed' ); | ||
Exception exceptionCaught; | ||
|
||
Test.startTest(); | ||
try | ||
{ | ||
ServiceUtils.logAndRethrow( exceptionToLog ); | ||
} | ||
catch( Exception e ) | ||
{ | ||
exceptionCaught = e; | ||
} | ||
Test.stopTest(); | ||
|
||
logger.assertNumberOfLogCalls( 0, 'logAndRethrow, when given an ortoo validation exception, will not log it' ); | ||
System.assertEquals( String.valueOf( exceptionCaught ), String.valueOf( exceptionToLog ), 'logAndRethrow, when given an ortoo validation exception, will rethrow it' ); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
framework/default/ortoo-core/default/classes/utils/tests/ServiceUtilsTest.cls-meta.xml
This file contains 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>52.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |