You need to sign in to do that
Don't have an account?
CodeFinder
Help with testing
Hi,
I have a exception log class which records all exceptions in a exception log object. I am trying to write test case for it. I have covered most of the class but I am not able to cover the block of code is catch box. Can anyone help em with a test code for the code in catch block?
public class ExceptionLogClass { public static void insertErrorLog(String errorIn, String severityIn, String errorSourceIn, String causedByUserIn) { Exception_Log__c eL = new Exception_Log__c(); try{ eL.Error__c = errorIn; eL.Severity__c = severityIn; eL.Error_Source__c = errorSourceIn; eL.Caused_by_User__c = causedByUserIn; insert eL; } catch(Exception e){ Apexpages.Message msg = new Apexpages.Message(Apexpages.Severity.Fatal, e.getMessage()); Apexpages.addMessage(msg); } } }
I am not sure how I should write test case code for the code in catch box. I get only 72% coverage becasue of it.
Here is the code which can cause the exception. Severity can only be "Fatal", "Error", "Info", "Warning" and "Confirm"
ExceptionLogClass.insertErrorLog('Error Test', 'Fat', 'Test ExceptionLog Class', Userinfo.getName());
Thank you.
Okay. I got the error handled. This is how I got it.
Thank you all for your help. Thanks alexb for the System.assert(boolean, pANY) statement. It dint strike my mind that I could use it this way too untill you used it in the code. I just used it in a different way. It looks much like a hard code .. but gives me 100% coverage.
All Answers
You would have to simulate an ApexPage that calls this method.
In your test method, try something like this:
PageReference pageRef = Page.MyPage;
Test.setCurrentPageReference(pageRef);
ExceptionLogClass.insertErrorLor(...params...);
//Now check the page for errors
List<ApexPages.Message> pageMessageList = ApexPages.getMessages();
System.assertEquals(1, pageMessageList.size());
System.assertEquals(true, pageMessageList.get(0).getSummary().contains('The error message you are expecting.'));
It looks to me like you can't get into the catch block, which means that you want to force inserting a new log to fail. Try this line:
ExceptionLogClass.insertErrorLog(null, null, null, null);
I'm not sure this will work, but basically you want to put in an input that will break it.
This is the test class. can you point out where I am going wrong?
it gives me a error Save Error:line 21:247 no viable alternative at character "
21:247 is at [Severity__c]
21:247 is at Severity__c
I have no idea what line it's complaining at. Paste a copy of the offending code.
I actually have a validation rule which check that the severity can only be one of the default five values. So if any other value than the default five is entered then the validation willthrow error. Here i am inserting "Fat" insted of Fatal which shoud throw error. I want to compare the e.getMessage().
For this class I commented out the lines in catch block. for those lines I get
Msg in my VF page
That is the last line where it has [Severity__c]. column 247 is at __
Okay. I got the error handled. This is how I got it.
Thank you all for your help. Thanks alexb for the System.assert(boolean, pANY) statement. It dint strike my mind that I could use it this way too untill you used it in the code. I just used it in a different way. It looks much like a hard code .. but gives me 100% coverage.