+ Start a Discussion
Apex developer 21Apex developer 21 

Unittest no coverage, why?

Im trying to make a unittest for my class but it gives me 0 coverage, please some help:
public class CreateInvoice{
    
    public CreateInvoice(){
        
        List<Training__c> trainingList = new List<Training__c>();
        List<Facturatie__c> facturatieList = new List<Facturatie__c>();
        
        try {
        trainingList = [SELECT id , NAME, Startdatum__c,factuur_bedrijf__c,
                          (SELECT Cursist_Prijs__c, Training__c, Cursist__c,CreatedDate,Prijs_training__c,
                           korting__c,Id 
                           FROM Trainingen_Volgen__r ) 
                       FROM  Training__c 
                       WHERE id NOT IN (SELECT  Training__c FROM Facturatie__c)];
        }catch (Exception e) {
            System.debug('The following exception has occurred' + e.getMessage() +
                         'At line number :' + e.getLineNumber() + 'Error' +
                         e.getStackTraceString());
        }              
        if(!trainingList.IsEmpty()){
            
            for(Training__c trainingRecord : trainingList){
                
                if(!trainingRecord.Trainingen_Volgen__r.IsEmpty()){
                    
                    for(Cursist__c cursistRecord : trainingRecord.Trainingen_Volgen__r){
                        
                        Facturatie__c facturatieRecord = checkDate(cursistRecord,trainingRecord);
                        facturatieList.add(facturatieRecord);
                    }
                }   
            }

            if(!facturatieList.IsEmpty()){  
               try {
                   insert facturatieList;
                }catch (Exception e) {
                    System.debug('The following exception has occurred' + e.getMessage() +
                                 'At line number :' + e.getLineNumber() + 'Error' +
                                 e.getStackTraceString());
                }   
               
            }
        }   
    }
    
    public Facturatie__c checkDate(Cursist__c cursistRecord,Training__c trainingRecord){
        
        if(cursistRecord != null && trainingRecord != null){
            Date todaysDate = system.today();
            
            if( todaysDate.addDays(-21) >= trainingRecord.Startdatum__c 
                && todaysDate >= cursistRecord.CreatedDate 
                && todaysDate.addDays(-1) >= trainingRecord.Startdatum__c){
                
                Facturatie__c facturatieRecord = new Facturatie__c();
                
                facturatieRecord.Training__c = trainingRecord.Id;   
                facturatieRecord.Factuur_Datum__c = todaysDate;  
                facturatieRecord.Verval_datum__c = todaysDate.addDays(30);
                
                if(cursistRecord.Prijs_training__c != null &&  cursistRecord.korting__c != null){
                    facturatieRecord.Factuur_Bedrag__c = cursistRecord.Prijs_training__c - cursistRecord.korting__c;
                }else if(cursistRecord.Prijs_training__c != null && cursistRecord.korting__c == null){
                    facturatieRecord.Factuur_Bedrag__c = cursistRecord.Prijs_training__c;
                }else if(cursistRecord.Prijs_training__c == null && cursistRecord.korting__c != null){
                    facturatieRecord.Factuur_Bedrag__c = cursistRecord.korting__c ;
                }
                
                facturatieRecord.Korting__c =  cursistRecord.korting__c;
                facturatieRecord.cursist_prijs__c= cursistRecord.Cursist_Prijs__c ;
                facturatieRecord.Contactpersoon__c = cursistRecord.Cursist__c;
                facturatieRecord.Account__c = trainingRecord.factuur_bedrijf__c; 
                
                return facturatieRecord;
            }
        }
        
        return null;
    }
}
 
@isTest
public class CreateInvoiceTestClass{
    
    // Creating Test Data
   @isTest public static void testData(){
    
        Account account = new Account(Name = 'TestAccount1');  
        insert account;
        
        List<Training__c> trainingList = new List<Training__c>();
        for(integer counter=0;counter<200;counter++){
            
            Training__c trainingRecord = new Training__c();
            trainingRecord.name = 'TestRecord'+counter;
            trainingRecord.Startdatum__c = System.today().addDays(+2);
            trainingRecord.factuur_bedrijf__c = account.Id;
            trainingList.add(trainingRecord);
        }
        insert trainingList;
        
         List<Facturatie__c> facturatieList = new List<Facturatie__c>();
        for(integer counter=0;counter<200;counter++){
            
            Facturatie__c facturatieRecord = new Facturatie__c();
            facturatieRecord.name = 'TestRecord'+counter;
            facturatieRecord.Factuur_Bedrag__c = 1000;
            facturatieRecord.Training__c = trainingList[counter].Id;
            facturatieList.add(facturatieRecord);
        }
        insert facturatieList;

       
        List<Cursist__c> cursistList = new List<Cursist__c>();
        for(integer counter=0;counter<200;counter++){
            
            Cursist__c cursistRecord = new Cursist__c();           
            cursistRecord.Training__c = trainingList[counter].Id;
            cursistRecord.Prijs_training__c = 10;
            cursistRecord.korting__c = 10;
            cursistRecord.CreatedDate = System.today();
            cursistList.add(cursistRecord);
        }
        insert cursistList;
       
      system.debug(facturatieList[0].Account__c);
       
      
       System.assertEquals(facturatieList[0].Account__c,null);
       
    }
   
}

 
Best Answer chosen by Apex developer 21
Amit Singh 1Amit Singh 1
Error is that you are not creating Instance of Your apec class. Use below code for test class.
@isTest
public class CreateInvoiceTestClass{
    
    // Creating Test Data
   @isTest public static void testData(){
    
        Account account = new Account(Name = 'TestAccount1');  
        insert account;
        
        List<Training__c> trainingList = new List<Training__c>();
        for(integer counter=0;counter<200;counter++){
            
            Training__c trainingRecord = new Training__c();
            trainingRecord.name = 'TestRecord'+counter;
            trainingRecord.Startdatum__c = System.today().addDays(+2);
            trainingRecord.factuur_bedrijf__c = account.Id;
            trainingList.add(trainingRecord);
        }
        insert trainingList;
        
         List<Facturatie__c> facturatieList = new List<Facturatie__c>();
        for(integer counter=0;counter<200;counter++){
            
            Facturatie__c facturatieRecord = new Facturatie__c();
            facturatieRecord.name = 'TestRecord'+counter;
            facturatieRecord.Factuur_Bedrag__c = 1000;
            facturatieRecord.Training__c = trainingList[counter].Id;
            facturatieList.add(facturatieRecord);
        }
        insert facturatieList;

       
        List<Cursist__c> cursistList = new List<Cursist__c>();
        for(integer counter=0;counter<200;counter++){
            
            Cursist__c cursistRecord = new Cursist__c();           
            cursistRecord.Training__c = trainingList[counter].Id;
            cursistRecord.Prijs_training__c = 10;
            cursistRecord.korting__c = 10;
            cursistRecord.CreatedDate = System.today();
            cursistList.add(cursistRecord);
        }
        insert cursistList;
       
       system.debug(facturatieList[0].Account__c);
       Test.StartTEst();
	   CreateInvoice createInv = new CreateInvoice();
	   Test.stopTest();
      
       System.assertEquals(facturatieList[0].Account__c,null);
       
    }
   
}
Let me know if this helps :)

Thanks!
Amit Singh