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
Alex YhapAlex Yhap 

How do I get around 'Methods defined as TestMethod do not support getContent call' on controller init for Apex Test Class


Test Class
@isTest
public class StandingOrdersControllerTest{    
    
    @isTest    
    public static void test0() { 
        PageReference pageRef = new PageReference('/apex/StandingOrders');
        Test.setCurrentPage(pageRef);  
        
        // 'Methods defined as TestMethod do not support getContent call' fix
        Blob content;
        if (Test.IsRunningTest()){
                content = Blob.valueOf('UNIT.TEST');
            }else{
                content = pageRef.getContent();
            } 
        
        // start test
        Test.startTest();
            // ===== error occurs here! =====
            StandingOrdersController controller = new StandingOrdersController();
           
           controller.PaymentDate = Date.today();        
            controller.startDate = Date.today();
            controller.endDate = Date.today();        
            String[] tempPicklist = controller.getRecurringTypePicklist();
            controller.getRecurringTypePicklist();
            controller.searchTransaction();
            controller.createPayments();
            controller.reload();
        Test.stopTest();
        
        
    }

    @isTest    
    public static void test1() { 
        PageReference pageRef = new PageReference('/apex/StandingOrders');
        Test.setCurrentPage(pageRef);  
    }
    
}
Controller
public class StandingOrdersController {
    
    Public List<selectoption> RecurringOptions {get;set;}
    Public String RecurringType {get;set;}
    Public List<selectoption> PaymentTypePicklist {get;set;}
    Public String PaymentType {get;set;}
    Public Date PaymentDate {get;set;}
    Public List<causeview__Gift__c> giftList {get;set;}
    Public List<causeview__Payment__c> paymentList {get;set;}
    Public Boolean multiCurrencyEnabled {get;set;}
    Public Date startDate { get; set; }
    Public Date endDate { get; set; }
    Time startTimeGMT = Time.newInstance(0, 0, 0, 0);  
    Time endTimeGMT = Time.newInstance(23, 0, 0, 0);   


    Public List<TransactionObj> TransactionObjList
    {get{
            if ( TransactionObjList == null ){
                TransactionObjList = new List<TransactionObj>();
                for (causeview__Gift__c g:giftList){TransactionObjList.add( new TransactionObj( g ) );}
            }
            return TransactionObjList;
        }
        private set;
    }    
    
    Public StandingOrdersController(){
        init();
    }

    Private void init(){
        RecurringType = 'Monthly';                                                          //Set default value for picklist
        PaymentDate = Date.today();
        RecurringOptions = initializeRecurringOptions();                                    //Initialize picklist
        PaymentTypePicklist = initializePaymentTypePicklist();
        //multiCurrencyEnabled = Schema.getGlobalDescribe().containsKey('CurrencyType');    //Check if Multi-Currency is enabled
        searchTransaction();
    }

    Public void searchTransaction(){
        giftList = new List<causeview__Gift__c>();        
        System.debug('startDate: '+startDate);
        String f =  'ID, Name, causeview__Organization__c, causeview__Organization__r.Name, causeview__Recurring_Donation__c, causeview__Reference__c, causeview__Recurring_Donation__r.Name, causeview__Recurring_Donation__r.causeview__Type__c, causeview__Recurring_Frequency__c, causeview__Constituent__c, causeview__Constituent__r.Name, causeview__Gift_Type__c, causeview__Expected_Amount__c,causeview__Next_Payment_Date__c';
        //if(multiCurrencyEnabled){f += ', CurrencyIsoCode ';}
        String c =  'WHERE causeview__Recurring_Donation__r.causeview__Type__c != \'ACH/PAD\' AND causeview__Recurring_Donation__r.causeview__Type__c != \'Credit Card\' AND causeview__Status__c = \'Active\'';
                if(startDate != null && endDate != null){
                    //2013-12-21T00:00:00Z                    
                    String beginningOfTime = dmlDate(startDate);
                    String endOfTime = dmlDate(endDate);
                    c += ' AND causeview__Next_Payment_Date__c >= '+beginningOfTime+' AND causeview__Next_Payment_Date__c <= '+ endOfTime;
                }
        if(!String.isBlank(PaymentType)) { c += ' AND causeview__Recurring_Donation__r.causeview__Type__c = \''+PaymentType+'\''; }
        
        String q = 'SELECT ' + f + ' FROM causeview__Gift__c ' + c + ' LIMIT 900';   
        System.debug(q);
        giftList = Database.Query(q);
        TransactionObjList = null;
        for(causeview__Gift__c g: giftList){ system.debug(g.Name + ' : ' + g.causeview__Next_Payment_Date__c); }
    }

    Public void createPayments(){
        //DEBUG
        System.debug('createPayments() paymentDate: ' + PaymentDate);
        paymentList = new List<causeview__Payment__c>();
        Id standardPaymentRecordTypeId =[SELECT Id FROM RecordType WHERE Name ='UK Regular Giving' AND SobjectType ='causeview__Payment__c' Limit 1].Id;

        DateTime date_time;
        if(PaymentDate == null){                
                System.debug('No payment date entered.');
            }else{
                for(TransactionObj t:TransactionObjList)
                {
                    if(t.selected){
                        causeview__Payment__c payment = new causeview__Payment__c();
                        payment.RecordTypeId = standardPaymentRecordTypeId;
                        //payment.causeview__Date__c = t.gift.causeview__Next_Payment_Date__c;
                        payment.causeview__Date__c = PaymentDate;
                        payment.causeview__Amount__c = t.gift.causeview__Expected_Amount__c;
                        payment.causeview__Status__c = 'Approved';
                        //payment.causeview__Payment_Type__c = '3rd-Party Payment';
                        if(!String.isBlank(PaymentType))
                        {                                
                            payment.causeview__Payment_Type__c = PaymentType;
                        }
                        payment.causeview__Donation__c = t.gift.Id;
                        paymentList.add(payment); 
                    }

                }
                try {insert paymentList;} catch (DmlException e) {
                    // Process exception here
                    System.Debug('Error inserting new payments.');
                } 
            }
                       
    }

    Public class TransactionObj {
        public causeview__Gift__c gift {get; set;}
        public Boolean selected {get; set;}
        public TransactionObj( causeview__Gift__c g ){
            gift = g;
            selected = false;
        }
    }
    
    Private List<selectoption> initializeRecurringOptions(){
        RecurringOptions = new list<selectoption>();
        RecurringOptions.add(new selectoption('BiWeekly','BiWeekly'));
        RecurringOptions.add(new selectoption('Monthly','Monthly'));
        return RecurringOptions;
    }

    Public PageReference reload(){
        PageReference pageRef = new PageReference(ApexPages.currentPage().getUrl());
        pageRef.setRedirect(true);
        return pageRef;          
    }

    // Looks at Picklist values for a specific record type. Use 3-rd party class to retrieve list.
    Private List<selectoption> initializePaymentTypePicklist(){
        PaymentTypePicklist = new list<selectoption>();
        Id recType2Id = [Select Id from RecordType Where SobjectType = 'causeview__Payment__c' AND DeveloperName like 'UK_Regular_Giving'].Id;
        List<string> options = PicklistDescriber.describe('causeview__Payment__c', recType2Id, 'causeview__Payment_Type__c');
        for(String pt:options){ PaymentTypePicklist.add(new selectoption(pt,pt)); }
        System.debug('PaymentTypePicklist: ' + PaymentTypePicklist);
        return PaymentTypePicklist;
    }

    //GET: Type Picklist
    public String[] getRecurringTypePicklist(){
        String[] typePicklist = new List<String>();
        Id recType2Id = [Select Id from RecordType Where SobjectType = 'causeview__Payment__c' AND DeveloperName like 'UK_Regular_Giving'].Id;
        List<string> options = PicklistDescriber.describe('causeview__Payment__c', recType2Id, 'causeview__Payment_Type__c');
        for(String pt:options){ typePicklist.add(pt); }    
        return typePicklist;
    }

    Public String dmlDate(Date d) {
        String month=String.valueOf(d.month());String day=String.valueOf(d.day());
        if(integer.valueOf(d.month())<=9){month='0'+d.month();}if(integer.valueOf(d.day())<=9) {day='0'+d.day();}
        return d.year()+'-'+month+'-'+day;
    }
}


 
Alex YhapAlex Yhap
'Methods defined as TestMethod do not support getContent call'

This error occurs when I try to instantiate the 'StandingOrdersController controller = new StandingOrdersController();'

Is there a workaround for this?