Skip to content

Commit

Permalink
Enhanced auto generated apex test failure logic
Browse files Browse the repository at this point in the history
See
#1
22
  • Loading branch information
afawcettffdc committed Mar 8, 2015
1 parent 8eacedc commit e646961
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
13 changes: 10 additions & 3 deletions rolluptool/src/classes/RollupController.cls
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ public with sharing class RollupController
// Display currently deployed code for confirmation
return RollupTriggerTest.Body;
else
{
// Namespace?
Schema.DescribeSObjectResult describe = LookupRollupSummary__c.sObjectType.getDescribe();
String name = describe.getName();
String localName = describe.getLocalName();
String namespace = name.removeEnd(localName).removeEnd('__');
// Deploy generated code
return
'/**\n' +
Expand All @@ -131,10 +137,11 @@ public with sharing class RollupController
'{\n' +
' private static testmethod void testTrigger()\n' +
' {\n' +
' // Code to cover the one line trigger, the method called has been tested separately by the packaged tests.\n' +
' try { insert new ' + RollupSummary.ChildObject__c + '(); } catch(Exception e) { }\n' +
' // Force the ' + RollupTriggerName + ' to be invoked, fails the test if org config or other Apex code prevents this.\n' +
' ' + (namespace.length() > 0 ? namespace + '.' : '') + 'RollupService.testHandler(new ' + RollupSummary.ChildObject__c + '());\n'+
' }\n' +
'}';
'}';
}
}

public String getTriggerCodeMetadata()
Expand Down
4 changes: 2 additions & 2 deletions rolluptool/src/classes/RollupControllerTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ private with sharing class RollupControllerTest
'{\n' +
' private static testmethod void testTrigger()\n' +
' {\n' +
' // Code to cover the one line trigger, the method called has been tested separately by the packaged tests.\n' +
' try { insert new ' + rollupSummary.ChildObject__c + '(); } catch(Exception e) { }\n' +
' // Force the ' + controller.RollupTriggerName + ' to be invoked, fails the test if org config or other Apex code prevents this.\n' +
' dlrs.RollupService.testHandler(new ' + rollupSummary.ChildObject__c + '());\n' +
' }\n' +
'}', controller.getTriggerTestCode());
System.assertEquals(
Expand Down
23 changes: 23 additions & 0 deletions rolluptool/src/classes/RollupService.cls
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,36 @@ global with sharing class RollupService
}
return masterRecords.values();
}

/**
* Apex Test handler (call from Apex Test only)
**/
global static void testHandler(SObject dummyChildRecord)
{
try {
insert dummyChildRecord;
} catch (Exception e) {
// If the auto generated trigger was invoked this test served its purpose (code coverage wise) ignore this error
if(triggerHandleInvoked)
return;
// Otherwise fail the test with the underlying exception as it prevented our trigger being invoked
throw e;
}
}

/**
* Used in a test context to determine if errors from the dummy child insert should fail the test
**/
private static boolean triggerHandleInvoked = false;

/**
* Apex Trigger helper, automatically resolves child records to process via LREngine and lookups described in LookupRollupSummary__c
* also determines if based on the old trigger records if the rollup processing needs to occur
**/
global static void triggerHandler()
{
triggerHandleInvoked = true;

// Currently no processing in the before phase
if(Trigger.isBefore)
return;
Expand Down

0 comments on commit e646961

Please sign in to comment.