1

Please find the class

global class EA_USIConsentForm {

public String officelocation { get; set; }

public String epid { get; set; }

public String ename { get; set; }

//public String renderAs { get; set; }

public string legalEntity{get;set;}

public ELE_Agreement__c pa{get;set;}

public ELE_Agreement__c ea;

public  attachment a{get;set;}

public transient Blob body{get;set;}

public transient Blob body1{get;set;}

public transient Blob body2{get;set;}

Public string name{get;set;}

public string name1{get;set;}

public string name2{get;set;}

public string pid{get;set;}

public Boolean opt1{get;set;}

public Boolean opt2{get;set;}

public Boolean opt3{get;set;}

public Boolean isCheck{get;set;}

public Boolean isCheck2{get;set;}

public Boolean isCheck3{get;set;}

public Boolean isUser{get;set;}

public List<Attachment> la;

public string response{get;set;}

public string opt;



public EA_USIConsentForm ()
{

  pid=ApexPages.CurrentPage().getParameters().get('id');

  User u=[Select id,accountID,ContactID,email from user where id=:userInfo.getUserId()];


  la=new List<attachment>();

 try
 {
  ea=[Select Acknowledgement_Date__c,email__c from ELE_Agreement__c where ID=:pid LIMIT 1];

   if(ea.email__c==u.email){

     isUser=true;

   }

   if(ea.Acknowledgement_Date__c!=null){

     // response='submitted';
   }


   pa=[Select id,Legal_Name_of_the_Employee__c,Legal_Entity__c,SAP_ID__c,contact__r.name, Office_Location__c from ELE_Agreement__c where id=:pid];

   opt=ApexPages.CurrentPage().getParameters().get('option');
   ename = pa.contact__r.name;
   epid = pa.SAP_ID__c ;
   legalEntity=pa.Legal_Entity__c;
   officelocation=pa.Office_Location__c;



    isCheck=false;
    isCheck2=false;


  }
  catch(Exception e)
  {

  }



}

 public pageReference p2;

 //public string statusMsg{get;set;}

public pageReference submit()
{

    if(ea.Acknowledgement_Date__c==null)
    {



    if(opt1==true&&opt2==true)
    { 



    pa.Consent_1__c=true;

    pa.Consent_2__c=true;       

    isCheck2=false;

    isCheck=true;
    upload();
    p2=page.PAThankYou;

    }

    if(opt3==true)
    {



      pa.Consent_3__c=true;

      isCheck2=true;

      isCheck=false;
      upload();

      p2=page.PAThankYou;  
    }


    pa.PA_EA__c='EA';
    pa.Candidate_Response__c=true;

    pa.Out_of_Office__c=false;

    //pa.Agreement_Status__c='Signed';

    pa.Acknowledgement_Date__c=Date.Today();

    //pa.Email_Received_Date__c=Date.Today();

    update pa;

        if(la.size()>0)
        {
            System.debug('### Attaching Uploads');
            insert la;

        }

   }
   else
   {

     p2=null;
   }

  return p2;

}

public PageReference upload()
{
  string ext='';
  string ext1='';
  integer atempsize;
  integer atemp1size;
  Attachment atemp=new attachment();
  Attachment atemp1 =new attachment();

  try 
  {
  if(opt3==true){

            ext = name1.substring(name1.indexOf('.'),name1.length());
            ext1 = name2.substring(name1.indexOf('.'),name2.length());

      system.debug('++++++++++'+ext);
      system.debug('+++++++++======='+ext1);

       atemp.parentID=pid;
       atemp.body=body1;
       atempsize = body1.size();     

       atemp1.parentID=pid;
       atemp1.body=body2;
       atemp1size = body2.size();

       atemp.name='Signed Consent Form'+ext;
       atemp1.name='Signed Employee Agreement'+ext1;

       la.add(atemp); 
       la.add(atemp1);

   }

   if(opt1==true && opt2==true){

       ext = name.substring(name.indexOf('.'), name.length());
       atemp.parentID=pid;
       atemp.body=body;
       atempsize = body.size(); 
       atemp.name='Signed Consent Form'+ext;
       la.add(atemp); 

    }
   }

    catch (Exception e) {
        Exception_Log__c errLog=WCT_ExceptionUtility.logException('PAConsentForm', 'PA ConsentForm', e.getMessage()+'   in:'+e.getStackTraceString()+ '   due to:'+e.getCause());

      }
      return null;
}



}

Test class:

             @istest
        public class EA_USIConsentFormTest 
        {
            static testmethod void testmethod1()
            {



                Test.startTest();
                ELE_Agreement__c objELE = new ELE_Agreement__c();
                objELE.Legal_Name_of_the_Employee__c ='Eswar';
              //  objELE.email__c = u.Email;
                objELE.SAP_ID__c='123';
                objELE.Legal_Entity__c = 'SFDC developer';
                objELE.Consent_1__c=true;
                objELE.Consent_2__c=true;
                objELE.Consent_3__c=true;
                objELE.Acknowledgement_Date__c = system.today();

                insert objELE; 

                Attachment attach=new Attachment();     
                attach.Name='Unit Test Attachment';
                Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
                attach.body=bodyBlob;
                attach.parentId=objELE.id;
                insert attach;

                ApexPages.CurrentPage().getParameters().put('id',objELE.Id);
                EA_USIConsentForm obj = new EA_USIConsentForm();

                obj.submit();
                obj.upload();

                objELE.Acknowledgement_Date__c=null;
                update objELE;



                EA_USIConsentForm  obj1 = new EA_USIConsentForm();

                obj1.opt1 =true;
                obj1.opt2 =true;
                obj1.opt3 =true;
                obj1.isUser=true;
                obj1.name1='attachment';
                obj1.name='attachment1';
                obj1.name2='attachment2';
                obj1.submit(); 


                Test.stopTest();
                }

        }

in above Apex class, few lines of code is not covering. I thing due to not insert attachment? if it is then can any one tell me how to insert attachment in test class properly?

below lines from class are not covering

ext1 = name2.substring(name1.indexOf('.'),name2.length());

      system.debug('++++++++++'+ext);
      system.debug('+++++++++======='+ext1);

       atemp.parentID=pid;
       atemp.body=body1;
       atempsize = body1.size();     

       atemp1.parentID=pid;
       atemp1.body=body2;
       atemp1size = body2.size();

       atemp.name='Signed Consent Form'+ext;
       atemp1.name='Signed Employee Agreement'+ext1;

       la.add(atemp); 
       la.add(atemp1);

   }

   if(opt1==true && opt2==true){

       ext = name.substring(name.indexOf('.'), name.length());
       atemp.parentID=pid;
       atemp.body=body;
       atempsize = body.size(); 
       atemp.name='Signed Consent Form'+ext;
       la.add(atemp); 
1

You're not setting name1 anywhere in your unit test, thus causing a NullPointerException. You should modify your controller to check for required values first and display an appropriate error otherwise. In your unit test, make sure you're setting all required values before calling submit/upload. Also, generally speaking, do not put try-catch blocks in your unit test unless you're explicitly testing for a thrown exception, and always use a specific exception subclass instead of Exception directly.

| improve this answer | |
  • @Frank Something like obj1.name1 = 'Some value'; – sfdcfox Mar 13 '18 at 18:23
  • I've update test class above with setting name and name1. Still not covering the part which I mentioned. I'm getting 73% – Frank Mar 14 '18 at 5:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.