2

My below test fails because of Run Failures: TestRangehoodObjectionSheet.RangehoodObjectionSheetTest Methods defined as TestMethod do not support getContent call. I added the Blob thinking it might resolve but it doesn't work. What could be the problem here? Have I used the Blob correctly?

@isTest
public with sharing class TestRangehoodObjectionSheet {
     static testMethod void RangehoodObjectionSheetTest() {
            String Build                = NBATRecords.getIdFromDeveloperName('Plan_Reviews__c','Building_Plan_Review');
            Account a = new Account(Name = 'Test1' ,Type = 'Lead Acct',BillingState = 'CA',Industry = 'Business Services');
            insert a;

            Establishment_Record__c er = new Establishment_Record__c(Account__c=a.Id ,Project_Name__c = 'TestPreet',PremiseStreet__c='Manhattan ave',PremiseBorough__c='Manhattan',PremiseState__c='NY',PremisePostalCode__c='11102',Referral_Source__c='311',Data_Source__c='CAMIS',Phase__c='Initiate',Business_Status__c='Open' );
            insert er;

            Application__c ap = new Application__c(Establishment_Record__c=er.Id,Job_Number__c='222222222');
            insert ap;

            Plan_Reviews__c pr = new Plan_Reviews__c(Application__c =ap.Id ,DOB_Job_Number__c='222222222',RecordTypeId =Build,Objections__c='Pipe size not indicated');
            insert pr;
            PageReference pageRef = Page.RangehoodObjectionSheet;

            Test.setCurrentPage(pageRef); 

            Blob content = Test.isRunningTest() ? Blob.valueOf('UNIT.TEST') : pageRef.getContent();

            Plan_Reviews__c              p   = new Plan_Reviews__c(id=pr.Id);



            ApexPages.StandardController sc  = new ApexPages.standardController(p);
            System.assertNotEquals(sc,null);
            RangehoodObjectionExtention    ext = new RangehoodObjectionExtention(sc);

            ext.Savecopy();

     }
}

Actual class for the test:

public with sharing class RangehoodObjectionExtention {
private final ApexPages.StandardController controller;
private final Id                           planreviewID;
public String imageURL{get;set;}
public List<String> objlist {get;set;} 
public string currentdate {get; set;}
   public RangehoodObjectionExtention (ApexPages.StandardController stdController) {
        controller   = stdController;
        planreviewID = (Id)controller.getRecord().get('Id');
        Date dt = Date.today();
        currentdate = dt.format();
        list<Plan_Reviews__c> prlist = [select FDNY_Objections__c from Plan_Reviews__c where id = :planreviewID limit 1];
        string obj;
        for(Plan_Reviews__c pr: prlist){
           obj = pr.FDNY_Objections__c;    
        }
        if(obj!=null){
           list<string> objections = obj.split(';'); 
           integer i =1;
           objlist = new list<string>();
           for(string o : objections){
              objlist.add(i++ +'. '+o);
           }
        }
        imageURL='/servlet/servlet.FileDownload?file=';
        List< document > documentList=[select name from document where Name='RangehoodObjectionLogo'];
        if(documentList.size()>0)
        {
           imageURL=imageURL+documentList[0].id;
        }   
   }     

   public PageReference Savecopy(){
      PageReference p = Page.RangehoodObjectionSheet;
      blob b = ApexPages.currentPage().getContentAsPDF ();
      Attachment a = new Attachment();
      a.body = b;
      a.name = 'Rangehood Objection sheet Issued '+currentdate+'.pdf';
      a.parentid = planreviewID;

      insert a;

      PageReference pref = new PageReference('/'+planreviewID);
      return pref;
   }
}
9

There was an update made in winter 16 which states that getcontent is also considered as a webservice Callout .So you may consider writing your test class using mock callouts .Never use get content inside test class

 @isTest
 public with sharing class TestRangehoodObjectionSheet {

    static testMethod void RangehoodObjectionSheetTest() {
        String build = NBATRecords.getIdFromDeveloperName('Plan_Reviews__c','Building_Plan_Review');
        Account a = new Account(Name = 'Test1' ,Type = 'Lead Acct',BillingState = 'CA',Industry = 'Business Services');
        insert a;

        Establishment_Record__c er = new Establishment_Record__c(Account__c=a.Id ,Project_Name__c = 'TestPreet',PremiseStreet__c='Manhattan ave',PremiseBorough__c='Manhattan',PremiseState__c='NY',PremisePostalCode__c='11102',Referral_Source__c='311',Data_Source__c='CAMIS',Phase__c='Initiate',Business_Status__c='Open' );
        insert er;

        Application__c ap = new Application__c(Establishment_Record__c=er.Id,Job_Number__c='222222222');
        insert ap;

        Plan_Reviews__c pr = new Plan_Reviews__c(Application__c =ap.Id ,DOB_Job_Number__c='222222222',RecordTypeId =Build,Objections__c='Pipe size not indicated');
        insert pr;
        PageReference pageRef = Page.RangehoodObjectionSheet;

        Test.setCurrentPage(pageRef); 

        Blob content == Blob.valueOf('UNIT.TEST');//Just use blob.value of .In your actual code you can add istestRunning

        Plan_Reviews__c p = new Plan_Reviews__c(id=pr.Id);
        
        ApexPages.StandardController sc  = new ApexPages.standardController(p);
        System.assertNotEquals(sc,null);
        RangehoodObjectionExtention    ext = new RangehoodObjectionExtention(sc);

        ext.Savecopy();

     }
 } 

Skip the Blob content in test class

public PageReference Savecopy(){

    PageReference p = Page.RangehoodObjectionSheet;
    blob b;
    if(!test.isRunningTest()){
      b = ApexPages.currentPage().getContentAsPDF ();
    }else{
      b = blob.valueof('TEST');
    }
    Attachment a = new Attachment();
    a.body = b;
    a.name = 'Rangehood Objection sheet Issued '+currentdate+'.pdf';
    a.parentid = planreviewID;

    insert a;

    PageReference pref = new PageReference('/'+planreviewID);
    return pref;
}
0

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.