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
Nathaniel Neblett 18Nathaniel Neblett 18 

Help with Test Class for Custom Controller used in visualforce page

Hey. I'm looking for some help with writing a test class for a custom controller that I am using in a VF page. The class code is below. The visualforce page is very light weight as it simply references the content in the static resources using the method in the controller. Everything works like a dream but I am struggling with creating the test class for the the controller.  Please help me write the test class as I am looking to deploy to a production org but cannot because the code coverage for the class is 0% 

Thanks 



public class RelNotesStaticResCtrl {

    public String textFromWinter20ReleaseNotesSummary
  {
    get {
        StaticResource sr1 = [
                select Body
                from StaticResource
                where Name = 'Winter20ReleaseNotesSummary'
                ];
        return sr1.Body.toString();
    }
}

    public String textFromCurrentReleaseNotesHTML
  {
    get {
        StaticResource sr2 = [
                select Body
                from StaticResource
                where Name = 'CurrentReleaseNotesHTML'
                ];
        return sr2.Body.toString();
    }
}




Best Answer chosen by Nathaniel Neblett 18
David Zhu 🔥David Zhu 🔥
You may use code below as reference.
@isTest
public class RelNotesStaticResCtrlTest {

    static testmethod void testController()
    {
        RelNotesStaticResCtrl ctrl = new RelNotesStaticResCtrl();
        string summary = ctrl.textFromWinter20ReleaseNotesSummary;
        //system.assert(summary == 'xxxxxx');
        string html = ctrl.textFromCurrentReleaseNotesHTML;
        //system.assert(html == 'yyyyyy');
    }

}


 

All Answers

David Zhu 🔥David Zhu 🔥
You may use code below as reference.
@isTest
public class RelNotesStaticResCtrlTest {

    static testmethod void testController()
    {
        RelNotesStaticResCtrl ctrl = new RelNotesStaticResCtrl();
        string summary = ctrl.textFromWinter20ReleaseNotesSummary;
        //system.assert(summary == 'xxxxxx');
        string html = ctrl.textFromCurrentReleaseNotesHTML;
        //system.assert(html == 'yyyyyy');
    }

}


 
This was selected as the best answer
Nathaniel Neblett 18Nathaniel Neblett 18
Thanks @David Zhu - You're an absolute legend ! Works a treat and with 100% code coverage too. Thank you