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
srikanth bellamkondasrikanth bellamkonda 

how to handle Account History in a test class to cover up code coverage

Hi, Am writing a test class to cover up code coverage, but the AccoutHistory lines are not coverd in the class, how to handle that.
Just assume that, I have a Class chat_piexyz, in that class i have a method 'PopulateChartData()' , In that method we have a if condition like
public With Sharing class Chat_piexyz {
 public Chat_piexyz (ApexPages.StandardController controller) {

// some data

}
Public void PopulateChartData()
{

  List<AccountHistory> accHistory = [Select oldvalue,newvalue, createddate from AccountHistory where AccountId =: accId AND field = 'M_AUM__c' ORDER BY createdDate ASC];
              
if(!accHistory.IsEmpty()){

// do some thing

}

}

But in Test class i wrote a class like this
@isTest
public class Chart_piedata_Test {
    static testMethod void chatContoller() {

        Account Acc = new Account();
        Acc.Name='Test123';

        acc.M_AUM__c = 4000;
         insert Acc; 
       
        acc.M_AUM__c = 20000;
        update acc; 
        system.debug('Test Account++'+acc);
        acc.M_AUM__c = 1000;
        
        update acc;
        system.debug('Update : '+[select oldvalue, newvalue, createddate from AccountHistory where (AccountId = :acc.id and field = 'M_AUM__c') order by createdDate]);
ApexPages.StandardController sc = new ApexPages.StandardController(acc);
         Chat_piexyz  controller =new chat_piexyz(sc);
 controller.PopulateChartData(); 

 }
}

But it is not covering the If condition(Account history) in the class Chat_piexyz.
I run the Query in Query log in developer console, it shows account history data With Respect To Account id.
pconpcon
It appears that it cannot be done in your unit tests [1].  You could work around this by "faking" the data in your controller if you are running a test.
 
public void populateChartData() {
    List<AccountHistory> accHistory = [select ... ];

    if (Test.isRunningTest()) {
        accHistory.add(new AccountHistory(...));
        accHistory.add(new AccountHistory(...));
    }

    // continued
}

[1] http://salesforce.stackexchange.com/questions/4007/is-it-possible-to-test-apex-that-relies-on-field-history-tracking