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
fiona gentryfiona gentry 

Need a test class for helper method

Dear Gurus,

I will be very grateful if you could provide me a test class for below apex code
public with sharing class MaintenanceRequestHelper {
    public static void updateWorkOrders(Map<Id,Case>applicableCases) {
        // TODO: Complete the method to update workorders
        System.debug('*******Inside MaintenanceRequestHelper Class*******');
        Map<Id, Integer> mapProduct = new Map<Id, Integer>(); 
        List<Case> newCaseList = new List<Case>();
        List<Product2> listProduct = [select Id, Maintenance_Cycle__c from Product2];                                   
        for (Product2 p : listProduct) {
            if (p != null) {
                if(p.Maintenance_Cycle__c != null){
                    mapProduct.put(p.Id, Integer.valueOf(p.Maintenance_Cycle__c));
                }               
            }
        }
        for(Case a: applicableCases.values()){
            Case newCase = new Case();
            newCase.Vehicle__c = a.Vehicle__c;
            newCase.Equipment__c = a.Equipment__c;
            newCase.Type = 'Routine Maintenance';
            newCase.Subject = String.isBlank(a.Subject) ? 'Routine Maintenance Request' : a.Subject;
            newCase.Date_Reported__c = Date.today();
            newCase.Status = 'New';
            newCase.Product__c = a.Product__c;
            newCase.AccountId = a.AccountId;
            newCase.ContactId = a.ContactId;
            newCase.AssetId = a.AssetId;
            newCase.Origin = a.Origin;
            newCase.Reason = a.Reason;
            newCase.Date_Due__c =  (mapProduct.get(a.Id) != null) ? (Date.today().addDays(Integer.valueOf(mapProduct.get(a.Id)))) : (Date.today());
                newCaseList.add(newCase);
        }
        if(newCaseList.size() > 0){
            insert newCaseList;
        }    
    }        
}

Sincere Thanks in Advance
Fiona
Best Answer chosen by fiona gentry
Anshul Bansal 23Anshul Bansal 23

Hello flona,

Please use below code.

@isTest
public class Test_MaintenanceRequestPositive {
    @isTest static void testPositive(){
        Product2 prod = new Product2();
        prod.Name = 'Test';
        prod.Maintenance_Cycle__c = 200;
        Insert prod;
        Case cs = new Case();
        cs.Status = 'New';
        cs.Origin = 'Web';
        cs.Equipment__c = prod.Id;
        cs.Subject ='Test Positive';
        Insert cs;
        cs.status = 'Closed';
        cs.Type = 'Routine Maintenance';
        update cs;
    }
    @isTest static void testNegative(){
        Product2 prod = new Product2();
        prod.Name = 'Test';
        prod.Maintenance_Cycle__c = 200;
        Insert prod;
        Case cs = new Case();
        cs.Status = 'New';
        cs.Origin = 'Web';
        cs.Subject ='Test Positive';
        Insert cs;
        cs.status = 'Closed';
        cs.Type = 'Routine Maintenance';
        update cs;
    }
    @isTest static void testBulk(){
        Product2 prod = new Product2();
        prod.Name = 'Test';
        prod.Maintenance_Cycle__c = 200;
        Insert prod;
        List<Case> caseList = new List<Case>();
        for(Integer i=0;i<300;i++){
            Case cs = new Case();
            cs.Status = 'New';
            cs.Origin = 'Web';
            cs.Subject ='Test Positive';
            cs.Equipment__c = prod.Id;
            caseList.add(cs);
        }
        Insert caseList;
        for(Case obj : caseList){
            obj.Type = 'Routine Maintenance';
            obj.Status = 'Closed';
        }
        update caseList;
    }
}


If this code is helpful for you. then Please mark as best answer.

Thanks