+ Start a Discussion
Apex developer 21Apex developer 21 

My unittest is only giving 30% please help it gives only coverage till line 55

public class BatchDossierInvoice implements Database.Batchable<sObject>,Schedulable  {

    public void execute(SchedulableContext SC) {
       Database.executeBatch(new BatchDossierInvoice(), 200); 
    }

    public Database.QueryLocator start(Database.BatchableContext info){
system.debug('line 3');  
     try{
       
                    String query = 'SELECT Id ,(SELECT Id , Betaald_max__c ,Betaald__c,Partij__c,Partij__r.AccountId '+
                                    'FROM Partijen__r ORDER BY Betaald__c DESC ), '+
                                    '(SELECT Id , Dossier__c,Totaal_Merlijn__c '+
                                    'FROM Declaraties__r) '+
                        'FROM Dossier__c '+
                        'WHERE Id IN (SELECT Dossier__c FROM Declaratie__c) '+
                        'AND createddate >= LAST_N_DAYS:30';
         
         
     
         return Database.getQueryLocator(query); 
         system.debug(query);
         
        }catch(Exception e ){
         
            System.debug('The following exception has occurred' + e.getMessage());
        } 
        return null;                    
    }
   
    public void execute(Database.BatchableContext bc, List<Dossier__c> dossierList){     
        
        if(!dossierList.IsEmpty() && dossierList != null){
            List<Facturatie__c> facturatieList = new List<Facturatie__c>();    
            
            for(Dossier__c dossierRecord : dossierList){              
                Decimal totalAmount = 0;
                Decimal UpdatedtotalAmount = 0;
                
                // calculate sum Totaal_Merlijn__c of Declaratie__c records     
                for(Declaratie__c DeclaratieRecord : dossierRecord.Declaraties__r){    
                    totalAmount = totalAmount + DeclaratieRecord.Totaal_Merlijn__c;
                }
 system.debug(totalAmount);               
                
                

                if(totalAmount != 0){                   
                    // Find Inkoop_Uren__c record having maximun Betaald_max__c  
                    Partij__c partijMaxBetaaldValue = dossierRecord.Partijen__r[0];
                    for(Partij__c partijRecord : dossierRecord.Partijen__r){                                          
                        if(   partijRecord.Betaald_max__c != null && partijMaxBetaaldValue.Betaald_max__c !=null
                           && partijRecord.Betaald_max__c >= partijMaxBetaaldValue.Betaald_max__c){                              
                            partijMaxBetaaldValue.Betaald_max__c = partijRecord.Betaald_max__c;
                        }                                           
                    }
                    
                    // Find Partij__c record having maximun Betaald_max__c create Facturatie__c 
                    if( partijMaxBetaaldValue != null){
                        Facturatie__c  facturatieRecord;
                        if(totalAmount <= partijMaxBetaaldValue.Betaald_max__c){
                            facturatieRecord = CreateInvoice(partijMaxBetaaldValue ,dossierRecord,totalAmount);
                            UpdatedtotalAmount =0;
                        }else{
                            facturatieRecord = CreateInvoice(partijMaxBetaaldValue ,dossierRecord,partijMaxBetaaldValue.Betaald_max__c);
                            UpdatedtotalAmount = totalAmount - partijMaxBetaaldValue.Betaald_max__c;
                        }
                        if(facturatieRecord != null){
                            facturatieList.add(facturatieRecord);
                        } 
                    }
                    
                    // For reaming record create Facturatie__c on basis of having maximun Betaald__c  
                    for(Partij__c partijRecord : dossierRecord.Partijen__r){                  
                        if(    partijMaxBetaaldValue != partijRecord && UpdatedtotalAmount != 0 
                            && partijRecord.Betaald__c != null && partijRecord.Betaald_max__c != null && UpdatedtotalAmount >= 0){
                            Double invoiceAmount =(totalAmount*(partijRecord.Betaald__c/100));
                            UpdatedtotalAmount = UpdatedtotalAmount - invoiceAmount;
                            
                            Facturatie__c  facturatieRecord = CreateInvoice(partijRecord,dossierRecord,invoiceAmount );                            
                            if(facturatieRecord != null){
                                facturatieList.add(facturatieRecord);
                            }                            
                        }
                    }
                }       
            }
            
            // insert newly created Facturatie__c records 
            if(!facturatieList.IsEmpty()){
system.debug('line 19'); 
                try { 
system.debug('line 20');  
                   database.insert(facturatieList,false);
                   System.debug('facturatieList'+facturatieList);
                }catch(Exception e ){
system.debug('line 21'); 
                     System.debug('The following exception has occurred' + e.getMessage() );
                } 
            }
        } 
    }
   
    // Create invoice records
    public Facturatie__c  CreateInvoice(Partij__c partijRecord,Dossier__c dossierRecord,Decimal invoiceAmount){
system.debug('line 22');        
        if(partijRecord != null && dossierRecord != null && invoiceAmount != null ){
system.debug('line 23');                       
            Facturatie__c facturatieRecord = new Facturatie__c();
            facturatieRecord.Dossier__c = dossierRecord.Id;
            facturatieRecord.Factuur_Datum__c = System.today();
            facturatieRecord.Verval_datum__c =  System.today().addDays(30);
            facturatieRecord.Factuur_Bedrag__c = invoiceAmount; 
            facturatieRecord.Contactpersoon__c = partijRecord.Partij__c;    
            facturatieRecord.Account__c = partijRecord.Partij__r.AccountId;   
                      
            return facturatieRecord;                                 
        } 
        return null;           
    }
    
    public void finish(Database.BatchableContext BC){
system.debug('line 24');        
    }
     
}
 
@isTest
public class BatchDossierInvoiceTestClass{
    @testSetup
    // Creating Test Data
    public static  void testData(){

        Account account = new Account(Name = 'TestAccount1');
        insert account;

        List<Dossier__c> dossierList = new List<Dossier__c>();
        for(integer counter=0;counter<200;counter++){

            Dossier__c dossierRecord = new Dossier__c();
            dossierRecord.Name = 'TestRecord'+counter;
            dossierRecord.Status__c = 'Klant';
            dossierRecord.Partijen__c = 'een team';
            dossierRecord.Datum_eerste_gesprek__c = System.today();
            dossierList.add(dossierRecord);
        }
        insert dossierList;

        List<Partij__c> partijList = new List<Partij__c>();
        for(integer counter=0;counter<200;counter++){
            Partij__c partijRecord = new Partij__c();
            partijRecord.Dossier__c = dossierList[counter].Id;
            partijRecord.Betaald_max__c = 100;
            partijRecord.Betaald__c = 10;
            partijList.add(partijRecord);
        }
        insert partijList;

        List<Partij__c> partijList2 = new List<Partij__c>();
        for(integer counter=0;counter<200;counter++){
            Partij__c partijRecord = new Partij__c();
            partijRecord.Dossier__c = dossierList[counter].Id;
            partijRecord.Betaald_max__c = 0;
            partijRecord.Betaald__c = 10;
            partijList2.add(partijRecord);
        }
        insert partijList2;

        List<Declaratie__c> DeclaratieList = new List<Declaratie__c>();
        for(integer counter=0;counter<200;counter++){
            Declaratie__c DeclaratieRecord = new Declaratie__c();
            DeclaratieRecord.Dossier__c =  dossierList[counter].Id;
            //DeclaratieRecord.Totaal_Merlijn__c = Totaal_Reisuren_Merlijn_new__c + Totaal_Reiskosten_Melijn_new__c + Totaal_Uren_Merlijn__c;
            DeclaratieList.add(DeclaratieRecord);
        }
        insert DeclaratieList;

    }@istest

    public static void  testschedule() {

        Test.StartTest();
        System.schedule('Scheduled Job 2', '0 0 * * * ?', new BatchDossierInvoice ());
        dateTime dt=System.now().addMinutes(1);
        String Csec,Cmin,Chr,Cday,Cmonth,CYear;
        Csec=String.valueof(dt.second());
        Cmin=String.valueof(dt.minute());
        Chr=String.valueof(dt.hour());
        Cday=String.valueof(dt.day());
        Cmonth=String.valueof(dt.month());
        CYear=String.valueof(dt.Year());
        String SchTimer=Csec+' '+Cmin+' '+Chr+' '+Cday+' '+Cmonth+' ? '+CYear;
        system.debug('*************SchTimer:'+SchTimer);
        BatchDossierInvoice  cas = new BatchDossierInvoice  ();
        system.schedule('Scheduler02: Running at', SchTimer, cas);
        Test.stopTest();
    }
}

 
Best Answer chosen by Apex developer 21
Apex developer 21Apex developer 21
Got it working! Was missing some testobjects. Here is the solution 89% coverage:
@isTest
public class BatchDossierInvoiceTestClass{
    @testSetup
    // Creating Test Data
    public static  void testData(){

        Account account = new Account(Name = 'TestAccount1');
        insert account;

        List<Dossier__c> dossierList = new List<Dossier__c>();
        for(integer counter=0;counter<200;counter++){

            Dossier__c dossierRecord = new Dossier__c();
            dossierRecord.Name = 'TestRecord'+counter;
            dossierRecord.Status__c = 'Klant';
            dossierRecord.Partijen__c = 'een team';
            dossierRecord.Datum_eerste_gesprek__c = System.today();
            dossierList.add(dossierRecord);
        }
        insert dossierList;

        List<Partij__c> partijList = new List<Partij__c>();
        for(integer counter=0;counter<200;counter++){
            Partij__c partijRecord = new Partij__c();
            partijRecord.Dossier__c = dossierList[counter].Id;
            partijRecord.Betaald_max__c = 100;
            partijRecord.Betaald__c = 10;
            partijList.add(partijRecord);
        }
        insert partijList;

        List<Partij__c> partijList2 = new List<Partij__c>();
        for(integer counter=0;counter<200;counter++){
            Partij__c partijRecord = new Partij__c();
            partijRecord.Dossier__c = dossierList[counter].Id;
            partijRecord.Betaald_max__c = 0;
            partijRecord.Betaald__c = 10;
            partijList2.add(partijRecord);
        }
        insert partijList2;

        List<Declaratie__c> DeclaratieList = new List<Declaratie__c>();
        for(integer counter=0;counter<200;counter++){
            
            Declaratie__c DeclaratieRecord = new Declaratie__c();
            DeclaratieRecord.Dossier__c =  dossierList[counter].Id;
            DeclaratieList.add(DeclaratieRecord);
        }
        insert DeclaratieList;
        
        
        List<Declaratie_Line_Item__c> DeclaratieLineItem = new List<Declaratie_Line_Item__c>();
        for(integer counter=0;counter<200;counter++){
            Declaratie_Line_Item__c DeclaratieLineRecord = new Declaratie_Line_Item__c();
            DeclaratieLineRecord.Aantal_Minuten__c = 100;
            DeclaratieLineRecord.Declaratie__c = DeclaratieList[counter].id;
            DeclaratieLineRecord.Directe_Bestede_Uren_Tarief_Merlijn__c = 1000;
            DeclaratieLineItem.add(DeclaratieLineRecord);
        }
        insert DeclaratieLineItem;
        

    }@istest

    public static void  testschedule() {

        Test.StartTest();
        System.schedule('Scheduled Job 2', '0 0 * * * ?', new BatchDossierInvoice ());
        dateTime dt=System.now().addMinutes(1);
        String Csec,Cmin,Chr,Cday,Cmonth,CYear;
        Csec=String.valueof(dt.second());
        Cmin=String.valueof(dt.minute());
        Chr=String.valueof(dt.hour());
        Cday=String.valueof(dt.day());
        Cmonth=String.valueof(dt.month());
        CYear=String.valueof(dt.Year());
        String SchTimer=Csec+' '+Cmin+' '+Chr+' '+Cday+' '+Cmonth+' ? '+CYear;
        system.debug('*************SchTimer:'+SchTimer);
        BatchDossierInvoice  cas = new BatchDossierInvoice  ();
        system.schedule('Scheduler02: Running at', SchTimer, cas);
        Test.stopTest();
    }
}

 

All Answers

LBKLBK
Can you post screenshot of which are all lines have been covered?

You can get it from "Overall Code Coverage" tray in Developer Console.
Apex developer 21Apex developer 21
Got it working! Was missing some testobjects. Here is the solution 89% coverage:
@isTest
public class BatchDossierInvoiceTestClass{
    @testSetup
    // Creating Test Data
    public static  void testData(){

        Account account = new Account(Name = 'TestAccount1');
        insert account;

        List<Dossier__c> dossierList = new List<Dossier__c>();
        for(integer counter=0;counter<200;counter++){

            Dossier__c dossierRecord = new Dossier__c();
            dossierRecord.Name = 'TestRecord'+counter;
            dossierRecord.Status__c = 'Klant';
            dossierRecord.Partijen__c = 'een team';
            dossierRecord.Datum_eerste_gesprek__c = System.today();
            dossierList.add(dossierRecord);
        }
        insert dossierList;

        List<Partij__c> partijList = new List<Partij__c>();
        for(integer counter=0;counter<200;counter++){
            Partij__c partijRecord = new Partij__c();
            partijRecord.Dossier__c = dossierList[counter].Id;
            partijRecord.Betaald_max__c = 100;
            partijRecord.Betaald__c = 10;
            partijList.add(partijRecord);
        }
        insert partijList;

        List<Partij__c> partijList2 = new List<Partij__c>();
        for(integer counter=0;counter<200;counter++){
            Partij__c partijRecord = new Partij__c();
            partijRecord.Dossier__c = dossierList[counter].Id;
            partijRecord.Betaald_max__c = 0;
            partijRecord.Betaald__c = 10;
            partijList2.add(partijRecord);
        }
        insert partijList2;

        List<Declaratie__c> DeclaratieList = new List<Declaratie__c>();
        for(integer counter=0;counter<200;counter++){
            
            Declaratie__c DeclaratieRecord = new Declaratie__c();
            DeclaratieRecord.Dossier__c =  dossierList[counter].Id;
            DeclaratieList.add(DeclaratieRecord);
        }
        insert DeclaratieList;
        
        
        List<Declaratie_Line_Item__c> DeclaratieLineItem = new List<Declaratie_Line_Item__c>();
        for(integer counter=0;counter<200;counter++){
            Declaratie_Line_Item__c DeclaratieLineRecord = new Declaratie_Line_Item__c();
            DeclaratieLineRecord.Aantal_Minuten__c = 100;
            DeclaratieLineRecord.Declaratie__c = DeclaratieList[counter].id;
            DeclaratieLineRecord.Directe_Bestede_Uren_Tarief_Merlijn__c = 1000;
            DeclaratieLineItem.add(DeclaratieLineRecord);
        }
        insert DeclaratieLineItem;
        

    }@istest

    public static void  testschedule() {

        Test.StartTest();
        System.schedule('Scheduled Job 2', '0 0 * * * ?', new BatchDossierInvoice ());
        dateTime dt=System.now().addMinutes(1);
        String Csec,Cmin,Chr,Cday,Cmonth,CYear;
        Csec=String.valueof(dt.second());
        Cmin=String.valueof(dt.minute());
        Chr=String.valueof(dt.hour());
        Cday=String.valueof(dt.day());
        Cmonth=String.valueof(dt.month());
        CYear=String.valueof(dt.Year());
        String SchTimer=Csec+' '+Cmin+' '+Chr+' '+Cday+' '+Cmonth+' ? '+CYear;
        system.debug('*************SchTimer:'+SchTimer);
        BatchDossierInvoice  cas = new BatchDossierInvoice  ();
        system.schedule('Scheduler02: Running at', SchTimer, cas);
        Test.stopTest();
    }
}

 
This was selected as the best answer