0

so I am newer(ish) to Apex still and clearly know that I am not getting any code coverage to my Apex class because I am not referencing the method, my question is how exactly would I go about calling/testing this method in my Apex class? I will post the code below, but a preface for this class is that I am trying to conditionally generate images inside of a table on a Visualforce page that I am using as Quote Template Content for Salesforce CPQ and believe it or not there is NO WAY(at least none I or my co-workers could find, XSL-FO REALLY does not like to cooperate) to do this other than coding it out. I realize this is a very tedious route to take just to generate some images, but getting better with Apex/Visualforce/Test Classes was my personal key takeaway here and keeping clients happy is always a good thing. It IS working as intended and I am very happy with it, but naturally, it's time for a test class.

Here is my code:

public with sharing class QuoteCheckboxes {

public List<SBQQ__Quote__c> quote {get; set;}

public String checkedBox {get; set;}
public String uncheckedBox {get; set;}

public String SlidingDoorsBox {get; set;}
public String SwingDoorsBox {get; set;}
public String RevolvingDoorBox {get; set;}
public String RevolvingDoorBoxCA {get; set;}
public String ICUBox {get; set;}
public String ManualDoorBox {get; set;}
public String TurnstileBox {get; set;}
public String SafetyCertificationBox {get; set;}


public QuoteCheckboxes(){

    Id quoteId = (Id)ApexPages.currentPage().getParameters().get('qid');
    System.debug('quoteId =' + quoteId);

    quote = [
    SELECT  Name,
            Quote_Class__c,
            Contract_Summary_Page_Customer__c,
            Legal_Contract__c,
            Schedule_B_Special_Conditions__c,
            Door_List_Line_Item_Order__c,
            Door_List_Grouped_By_Location__c,
            Signature_Page__c,
            Fax_Sheet__c,
            Covered_Parts__c,
            Inspection_Report_Swing_Door_LE__c,
            Inspection_Report_Swing_Door_HE__c,
            Inspection_Report_Sliding_Door__c,
            Inspection_Report_Revolving_Door__c,
            Inspection_Report_Manual_Door__c,
            Inspection_Report_Turnstile__c,
            AAADM_Safety_Inspection_Report__c,
            Inspection_Report_ICU_Door__c,
            Inspection_Report_Revolving_Door_CA__c
    FROM SBQQ__Quote__c
    WHERE Id = :quoteID
    ];

    checkedBox = *link*
    uncheckedBox = *link*

    System.debug('QUOTE PROPERTIES: ' + quote[0]);

    if(quote[0].Inspection_Report_Sliding_Door__c){
        SlidingDoorsBox = checkedBox;
    } else {
        SlidingDoorsBox = uncheckedBox;
    }

    if(quote[0].Inspection_Report_Swing_Door_HE__c){
        SwingDoorsBox = checkedBox;
    } else {
        SwingDoorsBox = uncheckedBox;
    }

    if(quote[0].Inspection_Report_Revolving_Door__c){
        RevolvingDoorBox = checkedBox;
    } else {
        RevolvingDoorBox = uncheckedBox;
    }

    if(quote[0].Inspection_Report_ICU_Door__c){
        ICUBox = checkedBox;
    } else {
        ICUBox = uncheckedBox;
    }

    if(quote[0].Inspection_Report_Manual_Door__c){
        ManualDoorBox = checkedBox;
    } else {
        ManualDoorBox = uncheckedBox;
    }

    if(quote[0].Inspection_Report_Turnstile__c){
        TurnstileBox = checkedBox;
    } else {
        TurnstileBox = uncheckedBox;
    }

    if(quote[0].AAADM_Safety_Inspection_Report__c){
        SafetyCertificationBox = checkedBox;
    } else {
        SafetyCertificationBox = uncheckedBox;
    }

    if(quote[0].Inspection_Report_Revolving_Door_CA__c){
        RevolvingDoorBoxCA = checkedBox;
    } else {
        RevolvingDoorBoxCA = uncheckedBox;
    }
}
}

Test Class:

@isTest
public with sharing class QuoteCheckboxesTest {

public static Id createTestQuote1(){

    SBQQ__Quote__c q1 = new SBQQ__Quote__c(

        Inspection_Report_Sliding_Door__c = true,
        Inspection_Report_Swing_Door_HE__c = true,
        Inspection_Report_Revolving_Door__c = true,
        Inspection_Report_ICU_Door__c = true,
        Inspection_Report_Manual_Door__c = true,
        Inspection_Report_Turnstile__c = true,
        AAADM_Safety_Inspection_Report__c = true,
        Inspection_Report_Revolving_Door_CA__c = true,
        Quote_Type__c = 'Service'

    );

    insert q1;

    return q1.Id;

}

public static Id createTestQuote2(){


    SBQQ__Quote__c q2 = new SBQQ__Quote__c(

        Inspection_Report_Sliding_Door__c = false,
        Inspection_Report_Swing_Door_HE__c = false,
        Inspection_Report_Revolving_Door__c = false,
        Inspection_Report_ICU_Door__c = false,
        Inspection_Report_Manual_Door__c = false,
        Inspection_Report_Turnstile__c = false,
        AAADM_Safety_Inspection_Report__c = false,
        Inspection_Report_Revolving_Door_CA__c = false,
        Quote_Type__c = 'Service'

    );

    insert q2;

    return q2.Id;

}

@isTest
static void testQuotes(){

    Id q1ID = createTestQuote1();
    Id q2ID = createTestQuote2();

}
}
1

So it doesn't appear that you're even testing your class. It only appears that you are testing the trigger, kind of, but never actually testing the class. To properly test your class, you should create an instance of your class, call the methods, assert the results of your methods. So while your test method is "passing" it's not testing anything. You're only calling these two methods within your test class that create a record, which will work the trigger, but never actually seeing if your class is in fact handling input/output accordingly.

To test your class you should construct it. then check that the values in your class are set accordingly. I would otherwise suggest moving your logic out of your constructor, it's not good practice to do more than initialization in the constructor. provide a method to execute on within your class and use that. Constructors are always named the same as the class. if you have nothing to set, the default one is provided without declaration. you can just simply name the class, place a method inside, and use it. or just make your method static if the situation deems it necessary. Judging by logic, you should just call a method inside instead. if this is for a VF Page, supply your method as an action parameter in the <apex:page> tag.

When testing a class with a page reference and parameters, you need to setup the process to know there is a page.

PageReference testRef = Page.<myPageName>;
testRef.getParameters().put( 'id', 'myValue' );
Test.setCurrentPageReference( testRef );

... construct your class and test your methods

Example: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm

| improve this answer | |
  • I seeee, I do realize I am not testing the class, I was wondering how I exactly would go about doing that in this case since there doesn't appear to be a method to call(at least not to me, but I'm not the greatest dev). The only way I could get this to compile in the first place was to name my constructor the same as my class, otherwise it just kept tossing errors about invalid constructor name that I couldn't clear. I'm sure there was a better way to have written this but I am still a rook and I'm sure I will figure that all out as time progresses. Thank you for your input! – Alarmed_Win Apr 4 at 19:53
  • 1
    I will update my answer with a suggestion here. – Ronnie Apr 4 at 20:01
  • 1
    Thank you, I got it to work! I need to refactor it a bit for a cleaner look and add in some System.asserts, but I can rest easy now that I have 100% and learned PLENTY of new things today, thank you again! – Alarmed_Win Apr 5 at 5:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.