Skip to content

Commit

Permalink
Improved comparing of exceptions in tests
Browse files Browse the repository at this point in the history
Added test for licensing service - that it logs exceptions properly
  • Loading branch information
rob-baillie-ortoo committed Apr 19, 2022
1 parent 3e3cffc commit 88d1827
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class TestLoggerService implements ILoggerService
}
}

public void assertCallIsLog( Integer logCallNumber, String assertionText )
public void assertCallIsLog( Integer logCallNumber, String assertionText )
{
System.assertEquals( 'log', methods[ logCallNumber ], assertionText );
}
Expand All @@ -95,6 +95,11 @@ public class TestLoggerService implements ILoggerService
System.assertEquals( expected, numberOfLogCalls, assertionText );
}

public void assertExceptionWasLogged( Integer logCallNumber, Exception expected, String assertionText )
{
ortoo_Asserts.assertEquals( expected, (Exception)parameters[ logCallNumber ][0], assertionText );
}

public void assertLogLevelIs( Integer logCallNumber, LoggerService.Level expected, String assertionText )
{
System.assertEquals( expected, (LoggerService.Level)parameters[ logCallNumber ][0], assertionText );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public with sharing class TestLoggerUtils
);
}

public static void switchLoggingToErrorOnly()
{
setLoggingLevels( new List<LoggerService.Level>{ LoggerService.Level.ERROR } );
}

public static void switchLoggingOff()
{
setLoggingLevels( new List<LoggerService.Level>() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public with sharing class SearchServiceImpl implements ISearchService
List<String> sortableFields = new List<String>();
try
{
return buildSearchConfiguration( searchConfigurationType ).getSortableFields();
sortableFields = buildSearchConfiguration( searchConfigurationType ).getSortableFields();
}
catch ( Exception e )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,18 @@ public with sharing class ortoo_Asserts
Boolean inRange = actual >= earliestExpected && actual <= latestExpected;
System.assert( inRange, assertionMessage + ' (looking for ' + earliestExpected + ' to ' + latestExpected + ', got ' + actual + ')' );
}

/**
* Assert that the given exceptions are the same.
*
* Takes care of the fact that exceptions can change object hash when they are rethrown
*
* @param Exception - The expected Exception
* @param Exception - The actual Exception
* @param String - The assertion message
*/
public static void assertEquals( Exception expected, Exception got, String assertionMessage )
{
System.assertEquals( String.valueOf( expected ), String.valueOf( got ), assertionMessage );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private without sharing class ServiceUtilsTest
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' );
ortoo_Asserts.assertEquals( exceptionCaught, exceptionToLog, 'logAndRethrow, when given an ortoo exception, will rethrow it' );

exceptionController.verify();
}
Expand All @@ -73,6 +73,6 @@ private without sharing class ServiceUtilsTest
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' );
ortoo_Asserts.assertEquals( exceptionCaught, exceptionToLog, 'logAndRethrow, when given an ortoo validation exception, will rethrow it' );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,37 @@ private with sharing class LicensingServiceImplTest

System.assert( productIsLicensed, 'productIsLicensed, when no license details exist in a sandbox, will return true' );
}

@isTest
private static void productIsLicensed_whenAServiceThrowsAnException_logsAndRethrowsIt() // NOPMD: Test method name format
{
TestLoggerService logger = TestLoggerUtils.registerTestLoggerService();
TestLoggerUtils.switchLoggingToErrorOnly();

Amoss_Instance mockOrganizationServiceController = ApplicationMockRegistrar.registerMockService( IOrganizationService.class );
Amoss_Instance mockLicenseSelectorController = ApplicationMockRegistrar.registerMockSelector( PackageLicense.sobjectType );

Exception expected = new Exceptions.DeveloperException( 'An exception' );

mockLicenseSelectorController
.when( 'selectSingleRow' )
.throws( expected );

Test.startTest();
Boolean productIsLicensed;
Exception got;
try
{
productIsLicensed = LicensingService.productIsLicensed();
}
catch( Exception e )
{
got = e;
}
Test.stopTest();

logger.assertNumberOfLogCalls( 1, 'productIsLicensed, when a called service throws an exception, will log it' );
logger.assertExceptionWasLogged( 0, expected, 'productIsLicensed, when a called service throws an exception, will log it' );
ortoo_Asserts.assertEquals( expected, got, 'productIsLicensed, when a called service throws an exception, will rethrow it' );
}
}

0 comments on commit 88d1827

Please sign in to comment.