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
Ankita Gupta 65Ankita Gupta 65 

apex specialist superbadge#Chanllenge4-Testing Automation Logic

I am working on developing the Test Class for Maintenence Request. All my assertions have passed, however the code coverage for trigger and its handler is 0. Below is my code. Any help would be highly appreciated.
//Trigger Code
trigger MaintenanceRequest on Case (before update, after update) {
  
  //  MaintenanceRequestHelper.updateWorkOrders();
    
  List<Case> caseList=new List<Case>();
    if(Trigger.isAfter)
    {
    for(Case req:Trigger.New)
    {
        
       if(req.Status == 'Closed' && (req.Type == 'Repair' || req.Type == 'Routine Maintenance'))
       {
           caseList.add(req);
       }
    }
    }
    MaintenanceRequestHelper.updateWorkOrders(caseList);
}

//Handler Code
public class MaintenanceRequestHelper {
    
    public static void updateWorkOrders(List<Case> requestList){
        // update workorders
         List<Case> cList=new List<Case>();
        Integer days;
        Date d=System.today();
        for(Case c:requestList)
        {
            List<Decimal> maintCycleList=new List<Decimal>();
            List<Work_Part__c> wkPart=[SELECT Id,Name,Equipment__c FROM Work_Part__c WHERE Maintenance_Request__c =:c.Id];
            for(Work_Part__c wp:wkPart)
           {
           List<Product2> equipList=[SELECT Name,Maintenance_Cycle__c FROM Product2 WHERE Id=:wp.Equipment__c];
               for(Product2 p:equipList)
               {
               maintCycleList.add(p.Maintenance_Cycle__c);
                   }
           }
          if(maintCycleList.size()>0)
          {
            maintCycleList.sort();
          
         Decimal maintCycle=maintCycleList.get(0);
        
         days=Integer.valueOf(maintCycle);
          }
            else
            {
             
         days=Integer.valueOf(0);   
            }
             Case newReq=new Case(Vehicle__c=c.Vehicle__c,Equipment__c=c.Equipment__c,Type='Routine Maintenance',Subject='New Request',Date_Reported__c=System.today(),Date_Due__c=d.addDays(days));
       
       cList.add(newReq);
        
       
        }
       insert cList; 
    }        
    
}

//Test Class
@isTest
private class TestMaintenanceRequest {
      
        
//Method to test trigger not working
    @isTest static void testMet1()
    {
      List<Case> caseList=new List<Case>();
        for(Integer i=0;i<300;i++)
        {
            Case c=new Case(Subject='TestSubject '+i);
            caseList.add(c);
        }
        test.startTest();
        insert caseList;
        test.stopTest();
        List<Case> assertCase=[SELECT Id FROM Case Where Subject Like 'TestSubject%' AND Status != 'Closed'];
        System.assertEquals(300,assertCase.size());
        
    }
    //Test method to test trigger working
    
    @isTest static void testMet2()
    {
        Product2 p=new Product2(Replacement_Part__c=true,Name='TestEquip',Maintenance_Cycle__c=5);
        insert p;
        Date d=System.today();
            Integer days=Integer.valueOf(p.Maintenance_Cycle__c);
        Vehicle__c v=new Vehicle__c(Name='TestVehicle');
        insert v;
        List<Case> caseList=[SELECT Id FROM Case WHERE Subject Like 'TestSubject%' AND Status != 'Closed' LIMIT 300];
        List<Case> updateList=new List<Case>();
        List<Work_Part__c> workPartList=new List<Work_Part__c>();
        for(Case c:caseList)
        {
            c.Status='Closed';
            c.Type='Repair';
            c.Vehicle__c=v.Id;
            Work_Part__c w=new Work_Part__c(Maintenance_Request__c=c.Id,Equipment__c=p.Id);
            updateList.add(c);
            workPartList.add(w);
        }
        test.startTest();
        try{
        Database.SaveResult[] insertResult=Database.insert(workPartList);
        for(Database.SaveResult sr:insertResult)
        {
            if(sr.isSuccess())
            {
                Database.SaveResult[] updateResult=Database.update(updateList);
                for(Database.SaveResult ur:updateResult)
          if(ur.isSuccess())
        {
            
         List<Case> assertNewCase=[SELECT Id FROM Case Where Equipment__c=:p.Id AND Vehicle__c=:v.Id AND Type='Routine Maintenance' AND Subject='New Request' AND Date_Reported__c=:d AND Date_Due__c=:d.addDays(days)];
        System.assertEquals(300,assertNewCase.size());
        }
            }
        }
        }
        catch(Exception e)
        {
            
        }
         test.stopTest();
        
        
    }
}

User-added image
Alain CabonAlain Cabon
Hi,

You didn't use  @testSetup static void methodName()  nor @isTest(SeeAllData=true) (not recommended for the superbadge) so you don't have any data for the test methods by default excepted for some specific objects so List<Case> caseList=[SELECT Id FROM Case WHERE Subject Like 'TestSubject%' AND Status != 'Closed' LIMIT 300]; should return no data because the methods are independants (and no @testSetup in your code )

Use test setup methods (methods that are annotated with @testSetup) to create test records once and then access them in every
test method in the test class. Otherwise you should recreate the test records in each method.

Moreover: Multiple test setup methods are allowed in a test class, but the order in which they’re executed by the testing framework isn’t
guaranteed.  
PAGE 563 / 2734 of the apex documentation (I read this documentation, a lot of pages more than 2500) but you can find a lot of important informations about the triggers in it.

Alain