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
llisallisa 

Help me on this test class please

My apex code is--

public with sharing class KHistoryController{  
    //Protected Members
    private static final DescribeSObjectResult oSomeObjectSchema = Schema.SObjectType.K_Document__c;
    private static final Map<string, Schema.SObjectField> mapFields = oSomeObjectSchema.fields.getMap();

    //Properties
    public Id SomeObjectId {get;set;}
    public integer PageSize {get;set;}
    public boolean AllowShowMore {get;set;}

    public List<SomeObjectHistory> SomeObjectHistories {
        get { return getSomeObjectHistory(SomeObjectId); }
    }

    //Constructors

    /**
     * Default Constructor
     */
    public KHistoryController() {
        PageSize = 5;   
        AllowShowMore = true;
    }

    //Public Methods
    public void showMore() {
        PageSize += 5;
    }

    //Private Methods

    /**
     * Returns SomeObject History records associated to the current SomeObject
     *
     * @param   SomeObjectId     the SomeObject__c record id to retrieve
     * @return  a list of SomeObjectHistory objects
     */
    private List<SomeObjectHistory> getSomeObjectHistory(Id SomeObjectId) {
        List<SomeObjectHistory> listSomeObjectHistory = new List<SomeObjectHistory>();

        if (SomeObjectId != null) {
            DateTime dLastCreatedDate = null;

            integer limitPlusOne = PageSize + 1;

            List<K_Document__History> listEntityHistory = [SELECT Id, Field, NewValue, OldValue, CreatedDate, CreatedById, CreatedBy.Name FROM K_Document__History WHERE ParentId = :SomeObjectId ORDER BY CreatedDate DESC, Id DESC LIMIT :limitPlusOne];
            AllowShowMore = (listEntityHistory.size() == limitPlusOne);

            for (K_Document__History oHistory : listEntityHistory) {
                SomeObjectHistory oSomeObjectHistory = new SomeObjectHistory(oHistory);

                if (mapFields.containsKey(oHistory.Field)) {
                    oSomeObjectHistory.FieldLabel = mapFields.get(oHistory.Field).getDescribe().Label;
                }

                if (dLastCreatedDate == oHistory.CreatedDate) {
                    oSomeObjectHistory.ShowDateAndUser = false;
                }
                else {
                    oSomeObjectHistory.ShowDateAndUser = true;
                }

                listSomeObjectHistory.add(oSomeObjectHistory);
                dLastCreatedDate = oHistory.CreatedDate;

                if (listSomeObjectHistory.size() == PageSize) break;
            }
        }

        return listSomeObjectHistory;
    }

    //Internal Classes

    /**
     * Data structure representing a SomeObject History record for display
     */
    public class SomeObjectHistory {
        //Properties
        public boolean ShowDateAndUser {get;set;}
        public string FieldLabel {get;set;}
        public K_Document__History History {get; private set;}

        public string ActionFormat {
            get { return getActionFormat(); }
        }

        public SomeObjectHistory(K_Document__History oHistory) {
            History = oHistory;
        }

        //Constructors
        public SomeObjectHistory() {
            showDateAndUser = true;
        }

        //Private Methods
        private string getActionFormat() {
            string sActionFormat = '';

            if (History != null) {
                sActionFormat = 'Record {0}.';

                if (History.newValue != null && History.oldValue == null) {
                    sActionFormat = 'Changed <strong>{1}</strong> to <strong>{3}</strong>.';    
                }
                else if (History.newValue != null && History.oldValue != null) {
                    sActionFormat = 'Changed <strong>{1}</strong> from {2} to <strong>{3}</strong>.';   
                }
                else if (History.Field != null && History.Field.equalsIgnoreCase('created')) {
                    sActionFormat = 'Created.';
                }
            }

            return sActionFormat;
        }
    }
}

and its test class is--

@isTest
private class test_KHistoryControllerTest 
{
    static testMethod void test_KHistoryController1()
    {
        K_Document__c k1 = new K_Document__c();
        k1.note__c = 'abcde';
        k1.Order_Number__c = '123';
        insert k1;
        
        k1.note__c = 'ABCD';
        update k1;
        
        //K_Document__History testHistroyrecord = new K_Document__History(field = 'Test History');//Please add any required field if it shows error
        //insert testHistroyrecord;

        Test.startTest();
        KHistoryController testController = new KHistoryController();
        testController.SomeObjectId = k1.id;
        testController.showMore(); 
        
        List<KHistoryController.SomeObjectHistory> testMethodCalling = testController.SomeObjectHistories;
        Test.stopTest();
    }
}

Here i got 42% of code coverage,please someone help me on this.


 
Best Answer chosen by llisa
Abhishek BansalAbhishek Bansal
Hi,

Please find code below :
 
@isTest
private class KHistoryControllerTest 
{
    static testMethod void test_KHistoryController1()
    {
		K_Document__c k1 = new K_Document__c();
        k1.note__c = 'abcde';
        k1.Order_Number__c = '123';
        insert k1;
        

		Test.startTest();
		KHistoryController testController = new KHistoryController();
		testController.SomeObjectId = k1.id;
		testController.showMore(); 
		
		List<KHistoryController.SomeObjectHistory> testMethodCalling = testController.SomeObjectHistories;
		
		KHistoryController.SomeObjectHistory objTestHistory = new KHistoryController.SomeObjectHistory();
		String testAction = objTestHistory.ActionFormat;
		Test.stopTest();
    }
}

Salesforce does not allow us to Insert History records in test class that is why your history records part will not be covered in any way.
Rest of the class can easily be covered with above code :)

Regards,
Abhishek.