function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Long TruongLong Truong 

Two almost indentical test class, one passed one failed

Someone else created a class and test class that works fine: CRequestForm_SearchController and CRequestForm_SearchController_test.
I made some edits to the class in sandbox, and copied the test class, so now I have a new class and a new test class: TESTCRequestForm_SearchController and TESTCRequestForm_SearchController_test. But I can't push it to production because the code coverage is only 68%. I have mannually tested it as an end-user and it works fine. Can anyone please help?
Best Answer chosen by Long Truong
Abhishek_PareekAbhishek_Pareek
Try this:
SFDC_IDs__c SSAIIDsRecord =  new SFDC_IDs__c();
SSAIIDsRecord.Name = 'SSAI';
SSAIIDsRecord.System_Administrator_Profile_ID__c = 'Id of system administrator profile in your org';
insert SSAIIDsRecord;

SFDC_IDs__c SSAIIDs = SFDC_IDs__c.getValues('SSAI');
 Id saProfileId = SSAIIDs.System_Administrator_Profile_ID__c;

All Answers

Long TruongLong Truong
public with sharing class TESTCRequestForm_SearchController {
  
  public TESTCRequestForm_SearchController(ApexPages.StandardController controller)
  { 
    cid=System.currentPageReference().getParameters().get('id');
    }
    
  Public String cid;                                                            
  public List<cContact> contactList {get; set;}                                 
  public boolean errormsg=false;
  public String errorDetail='';                                                
  String userinput1;                                                             
  String userinput2;
  String userinput3;                                                               

  Public boolean displayboxes;
  Public List<Contact> results = new List<Contact>();                                     

  Public List<Contact> selectedContactsstep2 = new List<Contact>();             
  Public List<Contact> cList = new List<Contact>();

  public String getuserinput1(){
    return userinput1;
    }
    
  public void setuserinput1(String userinp){
    this.userinput1=userinp;
    }
    
  public String getuserinput2(){
    return userinput2;
    }
    
  public void setuserinput2(String userinp){
    this.userinput2=userinp;
    }
    
  public String getuserinput3(){
    return userinput3;
    }
    
  public void setuserinput3(String userinp){
    this.userinput3=userinp;
    }
  public String geterrorDetail()
  { 
    return errorDetail;
  }  
    


  public List<Contact> contactsearch(){     
    errormsg=false;     
    contactList = new List<cContact>(); 
    if(userinput1=='' && userinput2=='' && userinput3=='')
    {
      errormsg=true;
      errorDetail='Please enter atleast one field to search for matching contact';
      contactList=null;
      return null;
    }    
    for(Contact c : [select Account.Name,Name,FirstName,LastName,Email,title,Id from Contact where FirstName like :userinput1+'%' and LastName like :userinput2+'%' and Account.Name like :userinput3+'%'])     
    {        
       contactList.add(new cContact(c));     
     } 
    return null;
    }

  public List<cContact> getresults()
  { 
    return contactList;
  }

  public class cContact{ 
    public Contact con {get; set;} 
    public Boolean selected {get; set;} 
    public cContact(Contact c) {     
    con = c;     
    selected = false; 
    }
  }

    
  public boolean geterrormsg()
  { 
    return errormsg;
  }

  
  public Pagereference createCRequestNew()
  {        
    Pagereference p =new Pagereference('/apex/CRequestNew'); 
           
    return p;
  }        
  
  public Pagereference createCRequest()
  {
    List<Contact> selectedContact = new List<Contact>(); 
    Pagereference p =new Pagereference('/a02/o');  
    if (contactList!= null)  {     
             
             for (cContact similarContact : contactList) {
                 if (similarContact.selected==true) {
                     selectedContact.add(similarContact.con);
                 }
         }
     if (selectedContact.size()==0) {     
      errormsg=true;
      errorDetail='Please select a contact to Modify';     
      return null; 
    } 
    else {     
      errormsg=false;
      String idField='';
      for (Contact selectedCont : selectedContact) {
      idField=selectedCont.Id;      
      }     
      p =new Pagereference('/apex/TEST_CRequestModify'+'?cid='+idField);     
      return p; 
    }        
    
  }
  return p;
  }
  public Pagereference createCRequestDelete()
  {
        List<Contact> selectedContact = new List<Contact>(); 
    Pagereference p =new Pagereference('/a02/o');  
    if (contactList!= null)  {     
             
             for (cContact similarContact : contactList) {
                 if (similarContact.selected==true) {
                     selectedContact.add(similarContact.con);
                 }
         }
     if (selectedContact.size()==0) {     
      errormsg=true;
      errorDetail='Please select a contact to Delete';     
      return null; 
    } 
    else {     
      errormsg=false;
      String idField='';
      for (Contact selectedCont : selectedContact) {
      idField=selectedCont.Id;      
      }     
      p =new Pagereference('/apex/TEST_Delete_contact_CRF'+'?cid='+idField);     
      return p; 
    }        
    
  }
  return p;


  }
  
  public Contact_Request_Form__c NewCRequest(Contact cContact)
    {
      
      Contact_Request_Form__c  c  = new Contact_Request_Form__c();
        c.Subgrantee_Name__c    =cContact.AccountId;           
        c.Last_Name__c       =cContact.LastName ;
      c.First_Name__c     =cContact.FirstName ;
       c.MI__c          =cContact.MI__c ;
      
       c.Fax_Phone__c      =cContact.Fax ;
       c.Mobile_Phone__c     =cContact.MobilePhone; 
       c.Home_Phone__c      =cContact.HomePhone ;
       c.Business_Phone__c    =cContact.OtherPhone ;
      
       c.Email_Address__c    =cContact.Email ;
       c.Job_Title__c      =cContact.Title; 
       c.Department_B__c      =cContact.Department;
       c.Department_S__c      =cContact.Shipping_Department__c;
       
       c.Organization_S__c    =cContact.Shipping_Organization__c;
       c.Organization_B__c    =cContact.Mailing_Organization__c;
           
      
       c.Address_Line_1B__c    =cContact.Contact_Mailing_Street_1__c;  
      c.Address_Line_2B__c    =cContact.Contact_Mailing_Street_2__c ; 
       c.City_B__c        =cContact.Contact_Mailing_City__c ; 
       c.Zip_Code_B__c      =cContact.Contact_Mailing_Zip_Code__c ; 
       c.Business_State__c    =cContact.Contact_Mailing_State__c  ;
      
       c.SSAI_Fiscal__c     =cContact.SSAI_Fiscals__c  ;
       c.SSAI_Spon__c      =cContact.SSAI_Sponsors__c  ;
       c.SSAI_Directors__c    =cContact.SSAI_Directors__c  ;
       c.D2D_Contacts__c      =cContact.D2D_Contacts__c  ;
      
       c.Address_Line_1S__c    =cContact.Contact_Shipping_Street_1__c;  
       c.Address_Line_2S__c    =cContact.Contact_Shipping_Street_2__c ; 
       c.City_S__c        =cContact.Contact_Shipping_City__c  ;
       c.Zip_Code_S__c      =cContact.Contact_Shipping_Zip_Code__c;  
       c.Shipping_State__c    =cContact.Contact_Shipping_State__c  ;
      
       //c.Fax_Phone__c      =cContact.Contact_Fax__c  ;
       c.Fax_Phone__c      =cContact.Fax  ;
       c.Contact__c      =cContact.Id;
       
       
       return c;
      
      
    }
    public Pagereference createCModify()
  {
  errormsg=false;
  String idField='';
  idField=cid;      
  Pagereference p  =new Pagereference('/apex/CRequestModify'+'?cid='+idField);     
  return p;
  }
  
  public Pagereference createCDelete()
  {
    Pagereference p =new Pagereference('/a02/o');       
    errormsg=false;
    Contact cContact=[Select Id,LastName,FirstName,MI__c,Fax,MobilePhone,HomePhone,OtherPhone,Email,Title,Contact_Mailing_Street_2__c
                        ,Contact_Mailing_City__c ,Contact_Mailing_Zip_Code__c ,SSAI_Sponsors__c,SSAI_Directors__c,Department,Contact_Mailing_Street_1__c ,
                        SSAI_Fiscals__c,D2D_Contacts__c,Contact_Shipping_Street_1__c ,Contact_Shipping_Street_2__c ,Contact_Shipping_City__c ,
                        Contact_Shipping_Zip_Code__c ,Contact_Fax__c ,Contact_Mailing_State__c ,Contact_Shipping_State__c,Shipping_Organization__c,
                        Mailing_Organization__c,Shipping_Department__c,AccountId from Contact where id =:cid limit 1];
        Contact_Request_Form__c newRec =NewCRequest(cContact);
        newRec.Status__c='Submitted';
        newRec.Is_Delete_Request__c=true;
    insert newRec;           
    return p;
  }
  
}

@isTest
private class TESTCRequestForm_SearchController_Test {

    static testmethod void constructor_Test() {
        Test.startTest();
        
        SFDC_IDs__c SSAIIDs = SFDC_IDs__c.getValues('SSAI');
        Id saProfileId = SSAIIDs.System_Administrator_Profile_ID__c;
        
        PageReference pageRef = Page.CRequestNew;
        Test.setCurrentPage(pageRef);
        
        User sau =
          [select UserRoleId
              from User
              where ProfileId = :saProfileId
               and isActive = true
               limit 1];
        
        System.runAs(sau) {       
        
        Account newAcc=new Account(Name='Test Account');
        insert newAcc;
        
        Contact_Request_Form__c newRec1 =new  Contact_Request_Form__c(Last_Name__c='Last Name Test',First_Name__c='First Name Test',Email_Address__c='TEST2@MAIL.COM',Status__c='Submitted');
        newRec1.Subgrantee_Name__c=newAcc.Id;
        insert newRec1;
        
        ApexPages.currentPage().getParameters().put('Id', newRec1.Id);
        
        Contact newContact=new Contact();
        newContact.LastName='Last Name';
        newContact.FirstName='First Name';
        newContact.AccountId=newAcc.Id;
        insert newContact;
        
        ApexPages.currentPage().getParameters().put('id', newContact.Id);
                       
        ApexPages.StandardController controller = new ApexPages.StandardController(newRec1);
        CRequestForm_SearchController WrapperC=new CRequestForm_SearchController(controller);
                        
        WrapperC.setuserinput3('T');
        WrapperC.contactsearch();
        
        pageRef = WrapperC.createCRequestNew();
        
        System.assertNotEquals(pageRef, null);
        
        pageRef = WrapperC.createCRequest();
        System.assertEquals(pageRef, null);
        pageRef = WrapperC.createCRequestDelete();
        pageRef = WrapperC.createCDelete();
        pageRef = WrapperC.createCModify();
        System.assertNotEquals(pageRef, null);
         }    
        Test.stopTest();
    }
    
}

Long TruongLong Truong
In the original class, when you click delete, a delete record is autmatically submitted. I created a new VF page to allow user to enter more information and then submit the delete record. The only change I made is in line 148 to reference to that new Delete VF Page.
Abhishek_PareekAbhishek_Pareek
Few changes in the testclass should help you in getting the code coverage issues. First of all, you need to insert data in SFDC_IDs__c custom setting as org's data is not available in testclass by default.So,
SFDC_IDs__c SSAIIDs = SFDC_IDs__c.getValues('SSAI');
Id saProfileId = SSAIIDs.System_Administrator_Profile_ID__c;
should be replaced by something like this:
SFDC_IDs__c SSAIIDsRecord =  new SFDC_IDs__c();
SSAIIDsRecord.System_Administrator_Profile_ID__c = 'Id of system administrator profile in your org';
insert SSAIIDsRecord;

SFDC_IDs__c SSAIIDs = SFDC_IDs__c.getValues('SSAI');
 Id saProfileId = SSAIIDs.System_Administrator_Profile_ID__c;
 
and also replace
ApexPages.StandardController controller = new ApexPages.StandardController(newRec1);
CRequestForm_SearchController WrapperC=new CRequestForm_SearchController(controller);
with
ApexPages.StandardController controller = new ApexPages.StandardController(newRec1);
TESTCRequestForm_SearchController WrapperC=new TESTCRequestForm_SearchController(controller);

Hope this is helpful.



Long TruongLong Truong
@Abhishek_Pareek Thanks for the suggestion. I made the changes, but there's a new error:

Class.TESTCRequestForm_SearchController_Test.constructor_Test: line 30, column 1. This is the code at line 30 :
insert SSAIIDsRecord;



System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name]
Abhishek_PareekAbhishek_Pareek
Try this:
SFDC_IDs__c SSAIIDsRecord =  new SFDC_IDs__c();
SSAIIDsRecord.Name = 'SSAI';
SSAIIDsRecord.System_Administrator_Profile_ID__c = 'Id of system administrator profile in your org';
insert SSAIIDsRecord;

SFDC_IDs__c SSAIIDs = SFDC_IDs__c.getValues('SSAI');
 Id saProfileId = SSAIIDs.System_Administrator_Profile_ID__c;

This was selected as the best answer
Long TruongLong Truong
@Abhishek_Pareek YOU DID IT! You the real MVP!!!! Thank you very much !!!!!