0

I have class using which i m generating the pdf and attaching it to the record.

Here is my apex class:

public with sharing class SFA_PL_AttachPDF_Ext {

private SFA_Customer_Activity_Report__c record;

public SFA_PL_AttachPDF_Ext(ApexPages.StandardController sc) 
{   
    //get current record details
    record = (SFA_Customer_Activity_Report__c) sc.getRecord();
    system.debug('current record=>>'+ record);
    System.Assert(record.Id != null);
}

 public PageReference init()
 {
    PageReference CAR_PDF;
    try
    {
        CAR_PDF = page.SFA_PL_FOC_Document;
        System.Assert(CAR_PDF != null);
        CAR_PDF.getParameters().put('id',record.Id);
        Blob pdfBlob = CAR_PDF.getContent();

        //create a new attachment
        Attachment attachMent  = new Attachment();
        system.debug('attachMent===>'+attachMent);

        attachMent.ParentId = record.Id;
        attachMent.Name = 'Product Confirmation ' + System.now() + '.pdf';
        attachMent.Body = pdfBlob;

        if(CAR_PDF!=NULL) 
        {
            //generate the attachment and redirect user on the same page
            insert attachMent;
            PageReference pageRef = new PageReference('/' + record.Id);
            pageRef.setRedirect(true);
            return pageRef;
        }
        else
        {
            return null;
        }
    }

    Catch(Exception ae)
    {
        system.debug('Exception is :::'+ae.getMessage());
        return null;
    }
 }
 }

below is my test class

@isTest(SeeAllData=false)
private class Test_SFA_PL_AttachPDF_Ext {

    static testMethod void myUnitTest() {

            Test_Functions tf = new Test_Functions('PL');
            tf.getRecordTypes();
            tf.initConfigurationSettings();
            tf.initCARSettings();
            system.debug('tf--->'+tf); 
            Account a1 = new Account();
            a1.RecordTypeId = tf.rtMap.get('SFA PL Accounts').Id;
            a1.Name = 'TEST';
            insert a1;
            system.debug('a1 list--->'+a1); 

            List<Contact> cons = new List<Contact>();
            Contact c1 = new Contact();
            c1.RecordTypeId = tf.rtMap.get('SFA PL Contacts').Id;
            c1.AccountId = a1.Id;
            c1.LastName = 'TEST';
            cons.add(c1);
            insert cons;
            system.debug('cons list--->'+cons); 

            SFA_Contract_Period__c cp1 = new SFA_Contract_Period__c();
            cp1.Contact__c = c1.Id;
            cp1.Contract_Type__c = 'Patient Survey Contract';
            cp1.From_Date__c = System.today().addDays(-5);
            cp1.To_Date__c = System.today().addDays(5);
            insert cp1;
            system.debug('cp1 list--->'+cp1);
            system.debug('*****************'+tf.rtMap);

            for(RecordType myMap : tf.rtMap.values())
            {
                System.debug('*************** Map values : ' + myMap);
            } 


            SFA_Customer_Activity_Report__c car = new SFA_Customer_Activity_Report__c();
            car.RecordTypeId = tf.rtMap.get('SFA PL Customer Activity Reports').Id;
            car.Contact__c = c1.Id;
            car.Date__c = System.today();
            car.Type__c = 'Special Customer Activity';
            car.Subtype__c = 'FSI Registration Card';
            car.Status__c = 'Completed';
            insert car;

            List<SFA_Customer_Activity_Report__c> newCAR = [select id, Date_PL__c 
                                                      from SFA_Customer_Activity_Report__c
                                                      where id =: car.id];

            ApexPages.StandardController sc = new ApexPages.StandardController(car);

            PageReference pref = Page.SFA_PL_FOC_Document;
            pref.getParameters().put('id',car.id);
            Test.setCurrentPage(pref);

            SFA_PL_AttachPDF_Ext testPdf = new SFA_PL_AttachPDF_Ext(sc);    

            Test.startTest();

            // populate the field with values
            Attachment attach=new Attachment();   
            attach.parentId = car.id;
            attach.Name = 'My Test PDF';

            // submit the record
            pref = testPdf.init();

            // assert that an attachment exists for the record
            //System.assertEquals(1,[select count() from attachment where parentId = :car.id]);

            Test.stopTest(); 
    }
}

the if block of my apex class is not getting covered.. the block in which attachment is inserted.

if(CAR_PDF!=NULL) 
            {
                //generate the attachment and redirect user on the same page
                insert attachMent;
                PageReference pageRef = new PageReference('/' + record.Id);
                pageRef.setRedirect(true);
                return pageRef;
            }

any help for it would be appreciated.. how shall i improve my test class?

2

Your Answer

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

Browse other questions tagged or ask your own question.