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 

Hello , some one please help me writing a test class on this.

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;
        }
    }
}
Best Answer chosen by llisa
Abhishek BansalAbhishek Bansal
Hi Monalisa,

I have updated the test class :
 
@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();
    }
}

Monalisa, It is very hard to guess and write a code for a class which we dont know what it is performing.
So i am just assuming that the code provided above will increase coverage of your code.
If it does not help you than i can help you by debugging your code in your Organisation.
So if it is possible for you than please share your org credentials.
You can also reach me at abhishek.bansal@metacube.com OR SkypeId : abhishek.bansal2790 if you need any help.

Regards,
Abhishek. 

All Answers

Abhishek BansalAbhishek Bansal
Hi,

Please use the below test class :
 
@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;
		Test.stopTest();
    }
}
Please make sure to run it by clicking "Run Test" button after saving the test class.

Regards,
Abhishek.
 
llisallisa
Hi Abhishek, thanks for replying,it is showing 16% code coverage.
these underline lines are not covered

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;
        }
    }
}
Abhishek BansalAbhishek Bansal
Hi,

Change your test class with below code :
@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<SomeObjectHistory> testMethodCalling = testController.SomeObjectHistories;
		Test.stopTest();
    }
}
Please run the test class again and see the code coverage.

Regards,
Abhishek
 
llisallisa
Hi, Ir gives me error like "Invalid type: SomeObjectHistory".
Abhishek BansalAbhishek Bansal
Please change line no. 15 i.e. List<SomeObjectHistory> testMethodCalling = testController.SomeObjectHistories;

To : List<KHistoryController.SomeObjectHistory> testMethodCalling = testController.SomeObjectHistories;

If that also does not work than please let me know i will provide you another solution.

Abhishek.
Abhishek BansalAbhishek Bansal
Which lines are not covered ?
llisallisa

It is working now and it is giving 42% code coverage.

those codes are not yet covered-


 /**
     * 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;
        }
    }
}
 
Abhishek BansalAbhishek Bansal
Please try below code :
 
@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;
		
		k1.note__c = 'ABCD';
		update k1;
		
		K_Document__History testHistroyrecord = new K_Document__History(Name = '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();
    }
}

Abhishek
llisallisa
Hi abhishek,
it will give the same 42% code coverage.
by adding that line 14 it is giving 0% coverage, here the code 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(id = '123');
        //List<K_Document__History> testHistroyrecord = new List<K_Document__History>();//Please add any required field if it shows error
        //testHistroyrecord.Name = 'Test History';
        //insert testHistroyrecord;
       

        Test.startTest();
        KHistoryController testController = new KHistoryController();
        testController.SomeObjectId = k1.id;
        testController.showMore(); 
        
        List<KHistoryController.SomeObjectHistory> testMethodCalling = testController.SomeObjectHistories;
        Test.stopTest();
    }
}
Abhishek BansalAbhishek Bansal
Hi Monalisa,

I have updated the test class :
 
@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();
    }
}

Monalisa, It is very hard to guess and write a code for a class which we dont know what it is performing.
So i am just assuming that the code provided above will increase coverage of your code.
If it does not help you than i can help you by debugging your code in your Organisation.
So if it is possible for you than please share your org credentials.
You can also reach me at abhishek.bansal@metacube.com OR SkypeId : abhishek.bansal2790 if you need any help.

Regards,
Abhishek. 
This was selected as the best answer