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
thomsantthomsant 

Test Class -Urgent..pls

Hi Need help to get the test class for the below controller .

 

Tom

 

public without sharing class RelAccExtn
{

    private list<Account> relacct;
    private Account acc;
    public boolean isCorrectRecordType
    {
        get{
             return new set<Id>{'012V00000000AQHIA2','01230000000npXrAAI'}.contains(acc.RecordTypeId);
           }
    }
    private list<Account> mRelatedAccounts;
    public list<Account> relatedAccounts{
        get {
            if (mRelatedAccounts==null) {
                mRelatedAccounts = new list<Account>();
                mRelatedAccounts.addAll([select AARP_Authorized_Agency__c,Account_Id__c,Account_No__c,
                Account__c,Agency_Business_Type__c,Agency_ID__c,Agency_Interest_Level__c,
                Agency_Loss_Notification__c,Agency_Model__c,Agency_Status__c,Agency__c,AIM__c,AnnualRevenue,
                Auto__c,A_Separate_Managed_Unit__c,BillingCity,BillingCountry,BillingPostalCode,BillingState,
                Marine_Region_Center__c,Marine_Underwriter_User__c,MARINE_UNDERWRITER__c,MasterRecordId,
                MM_Tech_Region_Center__c,MM_Tech_Underwriter_User__c,MM_TECH_UNDERWRITER__c,Monthly_Auto_Qt__c,
                Name,Nathometer__c,Needs_Batch_Processing__c,New_Accts_Under_25K__c,New_PL_WP__c,
                New_Sc_Count_25k__c,New_Sc_Count_75k__c,New_SC_WP_del__c,New__c,Non_Std__c,No_PL_Staff_Area__c,
                No__c,NumberOfEmployees,Number_of_Employees_del__c,Number_of_Producers__c,
              FROM Account where  ((EMC__c = :acc.EMC__c AND EMC__c != null) OR (DUNS_Nbr_Hdqtrs__c =:acc.DUNS_Nbr_Hdqtrs__c AND            DUNS_Nbr_Hdqtrs__c != null))
                  and Id != :acc.Id and Primary_Org__c != :acc.Primary_Org__c and RecordTypeId != :acc.RecordTypeId ]);
            }
            return mRelatedAccounts;
        }
    }
   
   public RelatedAccountExtension(ApexPages.StandardController std){
        std.addFields(new List<String>{'EMC__c','Type','DUNS_Nbr_Hdqtrs__c','Primary_Org__c','RecordTypeId'});
        this.acc = (Account)std.getRecord();
    }
}

Best Answer chosen by Admin (Salesforce Developers) 
kiranmutturukiranmutturu

only thing that u can do now is a change in your class

 public RelatedAccountExtension(ApexPages.StandardController std){
       if(!test.isRunningTest())//add this condition and check once

std.addFields(new List<String>{'EMC__c','Type','DUNS_Nbr_Hdqtrs__c','Primary_Org__c','RecordTypeId'});
        this.acc = (Account)std.getRecord();
    }

All Answers

kiranmutturukiranmutturu

try this 

@isTest
private class TestAccountSearch{
static testMethod void testgetSearchName(){


create a test account record and insert;

insert objaccount;


ApexPages.StandardController ApptController = new ApexPages.standardController(objAccount);
RelAccExtn objApptView = new RelAccExtn(ApptController);

boolean testisCorrectRecordType = objApptView.isCorrectRecordType;
list<account> actlist = objApptView.relatedAccounts;
}

}

thomsantthomsant

Hi Kiran

Tried the same way of thing but getting an error below is the test class.

@isTest
private class RelatedAccountExtension_Test {

    static testMethod void myUnitTest() 
    {
        Test.startTest(); 
        //RelatedAccountExtension a;
        //Boolean b = a.isCorrectRecordType;
        //List <Account> acc = new List<Account>();
        //acc = a.relatedAccounts;
        PageReference pageRef = Page.SF2SF_Related_Accounts;
        Test.setCurrentPageReference(pageRef);
        Account newAccount = new Account (name='Test',Type='Other',Primary_Org__c='PC',RecordTypeId='012V00000000AQHIA2',EMC__c='B0000000001-0099');
        insert newAccount;
            
        
        
        ApexPages.StandardController sc = new ApexPages.standardController(newAccount);

       // create an instance of the controller

        RelatedAccountExtension myPageCon = new RelatedAccountExtension(sc);
        Boolean b = myPageCon.isCorrectRecordType;
        List <Account> acc = new List<Account>();
        acc = myPageCon.relatedAccounts;
       
        
        Test.stopTest();  
        
    }
}

 

 

 

 

Tom

Method NameTotal Time (ms)MessageStack Trace

RelatedAccountExtension_Test.myUnitTest301.0System.SObjectException: You cannot call addFields when the data is being passed into the controller by the caller.Class.RelatedAccountExtension.<init>: line 79, column 9 Class.RelatedAccountExtension_Test.myUnitTest: line 71, column 45 External entry point
kiranmutturukiranmutturu

only thing that u can do now is a change in your class

 public RelatedAccountExtension(ApexPages.StandardController std){
       if(!test.isRunningTest())//add this condition and check once

std.addFields(new List<String>{'EMC__c','Type','DUNS_Nbr_Hdqtrs__c','Primary_Org__c','RecordTypeId'});
        this.acc = (Account)std.getRecord();
    }

This was selected as the best answer
thomsantthomsant

Thanks Kiran it sorted the issue.

 

Regards

Tom