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
Cris9931Cris9931 

I cant cover 75%, I only have 45 Test Class

Hi, can someone guide me how can i cover atleast 75 of this class?
 
public class WorkOrderRelatedList{
    
    public SVMXC__Service_Order_Line__c[] WorkDetailList{get;set;}
    String u = UserInfo.getUserId();
    public WorkOrderRelatedList(ApexPages.standardController std){
        WorkDetailList=new SVMXC__Service_Order_Line__c[]{};
         string WorkOrderId=ApexPages.currentPage().getParameters().get('id');
        if(WorkOrderId==NULL){
            system.debug('No Id available');
        }
        else{
            SVMXC__Service_Order__c[] AccWL=new SVMXC__Service_Order__c[]{};
            AccWL=[Select id,  (select ID, Name, lastmodifieddate, SVMXC__Line_Type__c,SVMXC__Product__c, SVMX_PS_Consumed_Qty__c, SIG_SC_Description__c, SVMXC__Group_Member__c, 
                                       SVMX_PS_Customer_Start_Date__c, SVMX_PS_Customer_Start_Time__c, SVMX_PS_Customer_End_Time__c, SVMX_PS_Customer_End_Date__c, SIG_Duration_Hours__c
                                from SVMXC__Service_Order_Line__r where (SVMXC__Line_Type__c = 'Labor' AND  CreatedById =:  u) OR (SVMXC__Line_Type__c = 'Travel' AND  CreatedById =:  u ) OR SVMXC__Line_Type__c = 'Parts' )  
                  from SVMXC__Service_Order__c where id =:WorkOrderId];
            for( SVMXC__Service_Order__c c:AccWL ){
                for(SVMXC__Service_Order_Line__c obj:c.SVMXC__Service_Order_Line__r ){
                    WorkDetailList.add(obj);
                }
            }
        }
    }
}



I only covered 45%...

 

@isTest
class WorkOrderRelatedList_UT{

    @isTest
    static void testtMethod() 
    { 
          String u = UserInfo.getUserId();
          SVMXC__Service_Order__c woTest = new SVMXC__Service_Order__c();
          woTest.SVMXC__Priority__c = 'Normal';
          insert woTest;
          
          SVMXC__Service_Order__c wo = [Select ID from SVMXC__Service_Order__c where Id =: woTest.id];
         
        
         ApexPages.StandardController sc = new ApexPages.StandardController(woTest);
         WorkOrderRelatedList testWo     = new WorkOrderRelatedList(sc);
        
         PageReference pageRef = Page.WorkDetailRelatedList; // Add your VF page Name here
         pageRef.getParameters().put('id', String.valueOf(wo.Id));
         Test.setCurrentPage(pageRef);
         
         SVMXC__Service_Order_Line__c workDetail = new SVMXC__Service_Order_Line__c();
         workDetail.SVMXC__Service_Order__c = wo.Id;
         workDetail.RecordTypeId = '0121i000000Gs2VAAS';
         workDetail.SVMXC__Line_Type__c = 'Labor';
         
         insert workDetail;
        
        
    }
}


My visualforce page:

 

<apex:page standardController="SVMXC__Service_Order__c" extensions="WorkOrderRelatedList" tabStyle="SVMXC__Service_Order_Line__c" lightningStylesheets="true">
<style>
body .bPageBlock .pbBody .labelCol{
     float:left;
}
</style>
    
    <apex:pageBlock Title="My Work Details">
        
            <apex:pageBlockTable style="width:100%" value="{!WorkDetailList}" var="n" >
                <apex:column style="width:50px" headerValue="Line Number" value="{!n.Name}"/>
                <apex:column style="width:50px" headerValue="Line Type" value="{!n.SVMXC__Line_Type__c}" />
                <apex:column style="width:10px" headerValue="Part" value="{!n.SVMXC__Product__c}" />
                <apex:column style="width:1px" headerValue="Consumed Qty" value="{!n.SVMX_PS_Consumed_Qty__c}" />
                <apex:column style="width:10px" headerValue="SC Description" value="{!n.SIG_SC_Description__c}" />
                <apex:column style="width:10px" headerValue="Technician" value="{!n.SVMXC__Group_Member__c}" />
                <apex:column style="width:10px" headerValue="Reported Start Date" value="{!n.SVMX_PS_Customer_Start_Date__c}" />
                <apex:column style="width:10px" headerValue="Reported Start Time" value="{!n.SVMX_PS_Customer_Start_Time__c}" />
                <apex:column style="width:10px" headerValue="Reported End Date" value="{!n.SVMX_PS_Customer_End_Date__c}" />
                <apex:column style="width:10px" headerValue="Reported End Time" value="{!n.SVMX_PS_Customer_End_Time__c}" />
                <apex:column style="width:10px" headerValue="Duration(hours)" value="{!n.SIG_Duration_Hours__c}" />


           
   </apex:pageBlockTable>
        
        
    </apex:pageBlock>    
    
</apex:page>

​​​​​​​
Best Answer chosen by Cris9931
Andrew GAndrew G
Based on what is not covered, I would say you are not picking up the Id from the controller. 

Try setting the page first, and setting the Id parameter with the Current Page method.
And then instantiate your controller.
@isTest
class WorkOrderRelatedList_UT{

    @isTest
    static void testtMethod() 
    { 
        String u = UserInfo.getUserId();
        SVMXC__Service_Order__c woTest = new SVMXC__Service_Order__c();
        woTest.SVMXC__Priority__c = 'Normal';
        insert woTest;

        SVMXC__Service_Order_Line__c workDetail = new SVMXC__Service_Order_Line__c();
        workDetail.SVMXC__Service_Order__c = woTest.Id; 
        workDetail.RecordTypeId = '0121i000000Gs2VAAS'; 
        workDetail.SVMXC__Line_Type__c = 'Labor'; 
        insert workDetail;

        Test.startTest();
      
        PageReference pageRef = Page.WorkDetailRelatedList; // Add your VF page Name here
        Test.setCurrentPage(pageRef);
        ApexPages.currentPage().getParameters().put('id', String.valueOf(woTest.Id));
        
        ApexPages.StandardController sc = new ApexPages.StandardController(woTest); 
        WorkOrderRelatedList testWo = new WorkOrderRelatedList(sc);

        Test.stopTest();

    }
}
Note, since we are just passing the Ids around, at this stage there is not need to do the extra step of extracting the inserted records just to use the Id.  Salesforce will populate the Id of the inserted record into memory to be used in the test class.  If there was a field that was populated by some automation on insertion, then you would need to query to get that value which is pushed to the database.  


regards
Andrew
 

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Cris - Can you highlight the lines that are not covered?
Cris9931Cris9931
Hi Anudeep,

sure.

User-added image
Andrew GAndrew G
Based on what is not covered, I would say you are not picking up the Id from the controller. 

Try setting the page first, and setting the Id parameter with the Current Page method.
And then instantiate your controller.
@isTest
class WorkOrderRelatedList_UT{

    @isTest
    static void testtMethod() 
    { 
        String u = UserInfo.getUserId();
        SVMXC__Service_Order__c woTest = new SVMXC__Service_Order__c();
        woTest.SVMXC__Priority__c = 'Normal';
        insert woTest;

        SVMXC__Service_Order_Line__c workDetail = new SVMXC__Service_Order_Line__c();
        workDetail.SVMXC__Service_Order__c = woTest.Id; 
        workDetail.RecordTypeId = '0121i000000Gs2VAAS'; 
        workDetail.SVMXC__Line_Type__c = 'Labor'; 
        insert workDetail;

        Test.startTest();
      
        PageReference pageRef = Page.WorkDetailRelatedList; // Add your VF page Name here
        Test.setCurrentPage(pageRef);
        ApexPages.currentPage().getParameters().put('id', String.valueOf(woTest.Id));
        
        ApexPages.StandardController sc = new ApexPages.StandardController(woTest); 
        WorkOrderRelatedList testWo = new WorkOrderRelatedList(sc);

        Test.stopTest();

    }
}
Note, since we are just passing the Ids around, at this stage there is not need to do the extra step of extracting the inserted records just to use the Id.  Salesforce will populate the Id of the inserted record into memory to be used in the test class.  If there was a field that was populated by some automation on insertion, then you would need to query to get that value which is pushed to the database.  


regards
Andrew
 
This was selected as the best answer
Cris9931Cris9931

Cheers Andrew. It worked! :)

 

I learn new thing today.