+ Start a Discussion
Bablu Kumar PanditBablu Kumar Pandit 

How to Write test Class of Vf contorller

Can Anyone Help I am first Time To write test Class of Vf controller so I have no idea How to write test class

Thanks Help Me
public class CancelProposalProtection {
    String caseId;
    Attachment attSign;
    public String source;
    public Boolean isPreveiew{set;get;}
    public String ipAddress{set;get;}
    public Case objCase{set;get;}
    public Account objAcc{set;get;}
    public String dateToday{set;get;}
    public String Sign1{set;get;}
    public String Sign2{set;get;}
    public String Sign3{set;get;}
    public String climbSignature{set;get;}
    public String applicantSignature{set;get;}
    public String coApplicantSignature{set;get;}
    public List<Attachment> lstSignatures;
    
    public CancelProposalProtection(){
        isPreveiew = false;
        attSign = new Attachment();
        dateToday = System.today().format();
        caseId = ApexPages.currentPage().getParameters().get('id');
        source = ApexPages.currentPage().getParameters().get('source');
        if(source == 'email')
            isPreveiew = true;
        
        try{
            if(!String.isBlank(caseId)){
                objCase = [Select Id,AccountId,CaseNumber,Status,Type,Contact.Name,CO_APPLICANT_NAME__c,Certificate_Number__c,Date_Purchased__c,
                             Creditor__c,Life__c,Disability__c,Acc_Dis_IIII__c,Reason_For_Cancellation__c FROM Case WHERE Id =: caseId ORDER BY LastModifiedDate DESC Limit 1];
                objAcc = [Select Id,PersonMailingstreet,PersonMailingCountry,PersonMailingPostalCode,PersonMailingState,PersonMailingCity,
                              Name,IsPersonAccount FROM Account WHERE Id =: objCase.AccountId ORDER BY LastModifiedDate DESC Limit 1];
                for(Attachment attSign : [Select Id,Name FROM Attachment WHERE Name LIKE '%Signature%' AND ParentId =: caseId ORDER BY LastModifiedDate DESC LIMIT 3]){
                    if(attSign.Name == 'ApplicantSignature.png'){
                        Sign1 = attSign.Id;
                    }if(attSign.Name == 'CoApplicantSignature.png'){
                        Sign2 = attSign.Id;
                    }if(attSign.Name == 'ClimbSignature.png'){
                        Sign3 = attSign.Id;
                    }
                }
                System.debug('----Signatures'+Sign1+'-------------Sign2'+Sign2);
            }
        }catch(Exception e){
            System.debug('----Exception'+e.getMessage());
        }
    }
    
    public void saveForm(){
        try{
            if(source == 'email'){
                //Case cs = new Case(Id=caseId,Agreement_Status__c='Signed',IP_Address__c=ipAddress,Cancellation_Form='In Progress');
                objCase.Agreement_Status__c = 'Signed';
                objCase.IP_Address__c = ipAddress;
                objCase.Cancellation_Form__c = 'Received';
                update objCase;
                System.debug('---------Signed case'+objCase);
                //If Signature already exist then delete it
                delete[Select Id FROM Attachment WHERE Name LIKE '%Signature%' AND ParentId =: caseId];
                //Checking if the Attachment already exist then delete it
                delete[Select Id FROM Attachment WHERE Name = 'Cancel Proposal Protection.pdf' AND ParentId =: caseId];
            
                //Inserting the Signatures
                lstSignatures = new List<Attachment>();
                if(String.isNotBlank(applicantSignature)){
                    Attachment signature1 = new Attachment(ParentId=objCase.Id, Name='ApplicantSignature.png', ContentType='image/png', Body=EncodingUtil.base64Decode(applicantSignature));
                    lstSignatures.add(signature1);
                }
                if(String.isNotBlank(coApplicantSignature)){
                    Attachment signature2 = new Attachment(ParentId=objCase.Id, Name='CoApplicantSignature.png', ContentType='image/png', Body=EncodingUtil.base64Decode(coApplicantSignature));
                    lstSignatures.add(signature2);
                }
                if(String.isNotBlank(climbSignature)){
                    Attachment signature3 = new Attachment(ParentId=objCase.Id, Name='ClimbSignature.png', ContentType='image/png', Body=EncodingUtil.base64Decode(climbSignature));
                    lstSignatures.add(signature3);
                }
                System.debug('---List Attachments'+lstSignatures);
                if(!lstSignatures.isEmpty())
                    insert lstSignatures;
                    
                saveAgreement(caseId);
            }else if(source == 'preview'){
                objCase.Agreement_Status__c = 'Send';
                objCase.Cancellation_Form__c = 'In Progress';
                update objCase;
                System.debug('---------Signed case'+objCase);
            }  
        }catch(Exception e){
            objCase.Exception_Log__c = e.getMessage()+'\n\n'+ e.getLineNumber() + '\n\n'+ String.valueof(e.getCause()); 
            update objCase;
        }
    }

    
    //Saving the VF Page as an Attachment
    @Future(callout=true)
    private static void saveAgreement(String caseId){
        try{
            if(String.isNotBlank(caseId)){
                System.debug('------Inside SaveForm');
                PageReference pg = new PageReference('https://climbcredit--360dcex--c.visualforce.com/apex/CancelProposalProtectionPdf?id='+caseId);
                //PageReference pg = new PageReference(Label.Site_Url+'/CancelProposalProtectionPdf?id='+caseId);
                Attachment att = new Attachment();
                att.Name = 'Cancel Proposal Protection.pdf';
                att.Body = pg.getContent();
                att.ParentId = caseId;
                insert att;
                System.debug('--att--'+att);
            }
        }catch(Exception e){
            Case cs = new Case();
            cs.Id = caseId;
            cs.Exception_Log__c = e.getMessage()+'\n\n'+ e.getLineNumber() + '\n\n'+ String.valueof(e.getCause()); 
            update cs;
        }
    }
}
Agustin BAgustin B
Hi Bablu check this links that will help you:
https://developer.salesforce.com/forums/?id=9060G000000I6E8QAK

If it helps please mark as correct, it may help others.