+ Start a Discussion
Scott BradyScott Brady 

Need help with test class - 31% coverage currently

Hi All,

I am in search of some assistance with helping me get a very simple controller into production.  I currently have 31% test coverage but I am running into issues confirming a page reference (and perhaps I may be missing something else to test to get to 75% coverage).  See my controller below:
 
public with sharing class dhrequestsInsertController
{
    public dhrequest__c requests {
        get {
            if(requests == null)
                requests = new dhrequest__c();
        return requests;
        }
        set;
   
    }
      
       
    public dhrequestsInsertController() {
    }
    
    public PageReference SubmitRequest() {
    try {
        insert requests;
        } catch (DMLException e) {
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new request. Please contact the Business Intelligence Team.'));
      }
      PageReference detailPage = new PageReference('/' + requests.id);
      return detailPage;
}


    public PageReference CancelRequest() {
        PageReference pr = new PageReference('/apex/dhrequests');
        pr.setRedirect(true);
        return pr;
}
}

Now here is my current test class in progress, sitting at 31% coverage at the moment.  Any tips would be most appreciated! Thank you very much in advance.
 
@isTest
public class dhrequestsInsertControllerTest {

public static testMethod void dhrequestsInsertControllerTest() {


    PageReference pageRef = Page.dhrequestdiscovery;
        Test.setCurrentPage(pageRef);
        
    dhrequestsInsertController controller = new dhrequestsInsertController();
                    
    dhrequest__c r = new dhrequest__c(dh_businesscase__c='test', dh_errormessage__c='error', dh_workaround__c='workaround');
    System.debug('business case before inserting: ' + r.dh_businesscase__c);   
        Insert r;
    
    String nextPage = controller.SubmitRequest().getUrl();
        System.assertEquals('/' + dhrequest__c.id, nextPage);    
    
        
    r = [SELECT dh_businesscase__c, id, dh_errormessage__c, dh_workaround__c FROM dhrequest__c WHERE id =:r.Id];
        System.debug('business case after inserting: ' + r.dh_businesscase__c); 
        System.debug('error after inserting: ' + r.dh_errormessage__c);
        System.debug('workaround after inserting: ' + r.dh_workaround__c);   
         
                      
    System.assertEquals('test', r.dh_businesscase__c);
    System.assertEquals('error', r.dh_errormessage__c);
    System.assertEquals('workaround', r.dh_workaround__c);    
        
        
    String lastPage = controller.CancelRequest().getUrl();
        System.assertEquals('/apex/dhrequests', lastPage);
        
  
    }
}


the SubmitRequest() pageref is giving me fits because I need to confirm that the record that is inserted, it's record Id is contained in the submitrequest redirect (to the detail page of the customer object).  I am getting mismatches there.  I also do not know how to replicate the error message itself, since it's really a catch all.  Any ideas?  Thanks again in advance.
 

Best,
Scott

Best Answer chosen by Scott Brady
Nayana KNayana K
@isTest
public class dhrequestsInsertControllerTest {

public static testMethod void dhrequestsInsertControllerTest() {


    PageReference pageRef = Page.dhrequestdiscovery;
        Test.setCurrentPage(pageRef);
        
    dhrequestsInsertController controller = new dhrequestsInsertController();
    dhrequest__c req = controller.requests;                
    controller.requests = new dhrequest__c(dh_businesscase__c='test', dh_errormessage__c='error', dh_workaround__c='workaround');
    
	// success
    String nextPage = controller.SubmitRequest().getUrl();
        System.assertEquals('/' + dhrequest__c.id, nextPage);    
    
        
    dhrequest__c r = [SELECT dh_businesscase__c, id, dh_errormessage__c, dh_workaround__c FROM dhrequest__c WHERE id =:controller.requests.Id];
        System.debug('business case after inserting: ' + r.dh_businesscase__c); 
        System.debug('error after inserting: ' + r.dh_errormessage__c);
        System.debug('workaround after inserting: ' + r.dh_workaround__c);   
         
    
	
    System.assertEquals('test', r.dh_businesscase__c);
    System.assertEquals('error', r.dh_errormessage__c);
    System.assertEquals('workaround', r.dh_workaround__c);    
        
    /* Trying to cover DML exception. Not populating any required field should cause error. 
	Assuming, atleast one among dh_businesscase__c, dh_errormessage__c, dh_workaround__c fields is required */	
	controller.requests = new dhrequest__c();
	nextPage = controller.SubmitRequest().getUrl();
	// error check
	System.assertEquals(true, ApexPages.getMessages().size() > 0);
	
    String lastPage = controller.CancelRequest().getUrl();
        System.assertEquals('/apex/dhrequests', lastPage);
        
  
    }
}

 

All Answers

Nayana KNayana K
@isTest
public class dhrequestsInsertControllerTest {

public static testMethod void dhrequestsInsertControllerTest() {


    PageReference pageRef = Page.dhrequestdiscovery;
        Test.setCurrentPage(pageRef);
        
    dhrequestsInsertController controller = new dhrequestsInsertController();
    dhrequest__c req = controller.requests;                
    controller.requests = new dhrequest__c(dh_businesscase__c='test', dh_errormessage__c='error', dh_workaround__c='workaround');
    
	// success
    String nextPage = controller.SubmitRequest().getUrl();
        System.assertEquals('/' + dhrequest__c.id, nextPage);    
    
        
    dhrequest__c r = [SELECT dh_businesscase__c, id, dh_errormessage__c, dh_workaround__c FROM dhrequest__c WHERE id =:controller.requests.Id];
        System.debug('business case after inserting: ' + r.dh_businesscase__c); 
        System.debug('error after inserting: ' + r.dh_errormessage__c);
        System.debug('workaround after inserting: ' + r.dh_workaround__c);   
         
    
	
    System.assertEquals('test', r.dh_businesscase__c);
    System.assertEquals('error', r.dh_errormessage__c);
    System.assertEquals('workaround', r.dh_workaround__c);    
        
    /* Trying to cover DML exception. Not populating any required field should cause error. 
	Assuming, atleast one among dh_businesscase__c, dh_errormessage__c, dh_workaround__c fields is required */	
	controller.requests = new dhrequest__c();
	nextPage = controller.SubmitRequest().getUrl();
	// error check
	System.assertEquals(true, ApexPages.getMessages().size() > 0);
	
    String lastPage = controller.CancelRequest().getUrl();
        System.assertEquals('/apex/dhrequests', lastPage);
        
  
    }
}

 
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
try to update your code like below
@isTest
public class dhrequestsInsertControllerTest {

public static testMethod void dhrequestsInsertControllerTest() {


    PageReference pageRef = Page.dhrequestdiscovery;
        Test.setCurrentPage(pageRef);
        
    dhrequestsInsertController controller = new dhrequestsInsertController();
                    
    dhrequest__c r = new dhrequest__c(dh_businesscase__c='test', dh_errormessage__c='error', dh_workaround__c='workaround');

    String lastPage = controller.CancelRequest().getUrl();
    System.assertEquals('/apex/dhrequests', lastPage);
	controller.requests = r;
	
		try
		{	
			String nextPage = controller.SubmitRequest().getUrl();
			System.assertEquals('/' + dhrequest__c.id, nextPage);    
		}
		Catch(Exception ee)
		{
		
		}
        
        
        
  
    }
}

Let us know if this will help you
 
Scott BradyScott Brady
Both answers were great - i didn't even think to test the error message - Nayana thank you for the comments in your code - nice to know i wasn't too far off - i did drop out the nextpage pageref to confirm that the url contains the id of the inserted record (get's me to 87%) - works for me since the visualforce page behind it works without issues.

Thanks again for your help everyone!