+ Start a Discussion
CodeFinderCodeFinder 

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.

Best Answer chosen by Admin (Salesforce Developers) 
CodeFinderCodeFinder

Okay. I got the error handled. This is how I got it.

 

 

@isTest
private class ExceptionLogClassTest {

	static testMethod void insertErrorLogTest(){
		
		ExceptionLogClass.insertErrorLog('Error Test', 'Fatal', 'Test ExceptionLog Class', Userinfo.getName());
		
		Exception_Log__c eLTest = [select Error__c, Severity__c, Error_Source__c, Caused_by_User__c  from Exception_Log__c where Error_Source__c = 'Test ExceptionLog Class' AND Severity__c = 'Fatal' limit 1];
		
		System.assertEquals('Error Test',eLTest.Error__c );
    	System.assertEquals('Fatal', eLTest.Severity__c);
    	System.assertEquals('Test ExceptionLog Class', eLTest.Error_Source__c);
    	System.assertEquals(Userinfo.getName(), eLTest.Caused_by_User__c);
		
		ExceptionLogClass.insertErrorLog('Error Test', 'Fat', 'ExceptionLogClassTest', Userinfo.getName());
		Integer i = [Select count() from Exception_Log__c where Error_Source__c = 'ExceptionLogClassTest'];
		System.assertEquals(0, i);
		eLTest =[select Error__c, Severity__c, Error_Source__c, Caused_by_User__c  from Exception_Log__c where Error_Source__c = 'ExceptionLog Class' limit 1];
		
		System.assert(true, eLTest.Error__c.contains('FIELD_CUSTOM_VALIDATION_EXCEPTION'));
    }
}

 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

alexbalexb

 

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.'));

 

Damien_Damien_

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.

CodeFinderCodeFinder

This is the test class. can you point out where I am going wrong?

 

@isTest
private class ExceptionLogClassTest {

	static testMethod void insertErrorLogTest(){
		
		ExceptionLogClass.insertErrorLog('Error Test', 'Fatal', 'Test ExceptionLog Class', Userinfo.getName());
		
		Exception_Log__c eLTest = [select Error__c, Severity__c, Error_Source__c, Caused_by_User__c  from Exception_Log__c where Error_Source__c = 'Test ExceptionLog Class'];
		
		System.assertEquals(eLTest.Error__c, 'Error Test');
    	System.assertEquals(eLTest.Severity__c, 'Fatal');
    	System.assertEquals(eLTest.Error_Source__c, 'Test ExceptionLog Class');
    	System.assertEquals(eLTest.Caused_by_User__c, Userinfo.getName());
		
		PageReference pageRef = Page.MyPage;
		Test.setCurrentPageReference(pageRef);
		ExceptionLogClass.insertErrorLor('Error Test', 'Fat', 'Test ExceptionLog Class', Userinfo.getName());
		//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('Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Severity can only be Fatal or Error or Confirm or Info or Warning: [Severity__c]'));
    }
}

 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

alexbalexb

I have no idea what line it's complaining at. Paste a copy of the offending code.

CodeFinderCodeFinder

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

 

  • Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Severity can only be Fatal or Error or Confirm or Info or Warning: [Severity__c]

 Msg in my VF page

 

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);            
            ExceptionLogClass.insertErrorLog(e.getMessage(), 'Fatal', 'ExceptionLog Class', Userinfo.getName());
        }
    }
    
}

 

CodeFinderCodeFinder

That is the last line where it has [Severity__c]. column 247 is at __

CodeFinderCodeFinder

Okay. I got the error handled. This is how I got it.

 

 

@isTest
private class ExceptionLogClassTest {

	static testMethod void insertErrorLogTest(){
		
		ExceptionLogClass.insertErrorLog('Error Test', 'Fatal', 'Test ExceptionLog Class', Userinfo.getName());
		
		Exception_Log__c eLTest = [select Error__c, Severity__c, Error_Source__c, Caused_by_User__c  from Exception_Log__c where Error_Source__c = 'Test ExceptionLog Class' AND Severity__c = 'Fatal' limit 1];
		
		System.assertEquals('Error Test',eLTest.Error__c );
    	System.assertEquals('Fatal', eLTest.Severity__c);
    	System.assertEquals('Test ExceptionLog Class', eLTest.Error_Source__c);
    	System.assertEquals(Userinfo.getName(), eLTest.Caused_by_User__c);
		
		ExceptionLogClass.insertErrorLog('Error Test', 'Fat', 'ExceptionLogClassTest', Userinfo.getName());
		Integer i = [Select count() from Exception_Log__c where Error_Source__c = 'ExceptionLogClassTest'];
		System.assertEquals(0, i);
		eLTest =[select Error__c, Severity__c, Error_Source__c, Caused_by_User__c  from Exception_Log__c where Error_Source__c = 'ExceptionLog Class' limit 1];
		
		System.assert(true, eLTest.Error__c.contains('FIELD_CUSTOM_VALIDATION_EXCEPTION'));
    }
}

 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. 

This was selected as the best answer