I am doing some test code right now. In my Apex code I have a method that try-catches a DMLException and afterwards does a general Exception catch.
In my test code I am successfully managing to test both(they come out as success), however salesforce doesn't acknowledge the test for the general exception.
Do you have any ideas? Thank you!
Here's my method code:
public void createTimeSheet() {
errorMsg = '';
TimeSheet__c newTimeSheet = new TimeSheet__c(
Date__c = date.valueOf(newTimeSheetDate),
Status__c = 'New',
Employee__c = [SELECT Id FROM Employee__c WHERE Name=:EmployeeName].Id
);
try {
insert newTimeSheet;
//catching the DML exceptions first
} catch(DmlException e) {
errorMsg = e.getDmlMessage(0);
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,errorMsg));
//catching all other exceptions
} catch(Exception e) {
errorMsg = e.getMessage();
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,errorMsg));
}
}
And here's my test method code:
public static testMethod void createTimeSheetNullPointerFail_test() {
init();
cont.EmployeeName = testEmployeeName;
cont.tslist = testTsList;
test.startTest();
cont.createTimeSheet();
test.stopTest();
System.assert(cont.errorMsg.contains('System.NullPointerException: Attempt to de-reference a null object'), cont.errorMsg);
}
try
block?