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
Pavani Akella 9Pavani Akella 9 

Help writing test classes

Can someone please please help me writing test classes. It's urgent.

public without sharing abstract class XmlPayloadController {
    public static final String XML_HEADER { get; } 
    
    public Case theCase { get; private set; }
    
    static { 
        XML_HEADER = '<?xml version="1.0" ?>';
    }
    
    public virtual String getBusinessUnit() {
        for (Replacement_Part__c rp : theCase.Replacement_Parts__r) {
            if (String.isNotBlank(rp.Replacement_Item__r.Business_Unit__c)) {
                return rp.Replacement_Item__r.Business_Unit__c;
            }
        }
        return null;
    }
    
    public virtual String getDeptId() {
        String deptId = '';
        String businessUnit = getBusinessUnit();
        if (businessUnit == 'M140') {
            deptId = '170';
        }
        else if (businessUnit == 'M170') {
            deptId = '172';
        }
        return deptId;
    }
    
    protected void loadCase(Id caseId) {
        // TODO: add new custom fields when they become available
        theCase = [SELECT Id, CaseNumber,
                          Account.PeopleSoft_Customer_ID__c,
                          Account.PeopleSoft_Vendor_ID__c,
                          Ship_To_Address__r.Sequence_Number__c,
                   Business_Unit__c
                    FROM Case WHERE Id = :caseId];
    }
}
Raj VakatiRaj Vakati
Its an  abstract class so Write the test class for the extended class by calling this methods from there 

http://www.infallibletechie.com/2014/04/test-class-for-abstract-class-in.html
Raj VakatiRaj Vakati
Some thing like below
public class XmlPayloadControllerCntl extends XmlPayloadController{
	
	
	
}



@isTest
private class XmlPayloadControllerCntlTest{
    static testMethod void test(){
		// insert Replacement_Part__c data 
		// insert casedata 
        XmlPayloadControllerCntl sa = new XmlPayloadControllerCntl();  
           
        sa.getDeptId() ;
		 sa.loadCase(case.Id) ;
    }
}

 
Pavani Akella 9Pavani Akella 9
Thanks Raj for the response. Do I need to create a test class for an abstract class? There are 3 Apex classes which extends XMLPayloadcontroller class and all the 3 apex classes have a test class.
Raj VakatiRaj Vakati
No need to create a test class for abstract class.. Call those methods form the any one of the extended class .. 

Refer this link 


http://www.infallibletechie.com/2014/04/test-class-for-abstract-class-in.html