![This is the line of code where it shows error](https://i.stack.imgur.com/q3WKU.jpg) 

I am new in SFDC i need to get 100% code coverage for this batch class currently it has 17% and i am facing problem in every `Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();`as its showing red in developer console see below the 

**Batch Class**

===============================================================================

```
global with sharing class InvoiceAutomationClass1 implements Database.Batchable<Sobject>
        {
            <b>public List<EmailTemplate> etemp {get;set;}
            public List<EmailTemplate> etemps {get;set;}
            public List<EmailTemplate> etempnuk {get;set;}
            public List<EmailTemplate> etemptech {get;set;}
            public List<EmailTemplate> etemprgtech{get;set;}</b>
            global  List<Id> Invsend = new List<Id> ();
            global  List<Id> ListInv = new List<Id> ();
            global  List<Id> ListInv1 = new List<Id>();
            global  List<Id> Invrem = new List<Id> ();

        global Database.Querylocator start(Database.BatchableContext BC)

        {
           return Database.getQueryLocator([Select Ageing__c,Invoice_Billing_Entity__c,Status_Reference__c,Contact_Person__c,Contact_Email__c,Invoice_Email_Status__c from Invoice__c where (Ageing__c=-26 And Status_Reference__c= 'Raised' And Invoice_Email_Status__c= 'Invoice Ok To Send') 
                                                                                    or (Ageing__c=-12 And Status_Reference__c= 'Raised' And Invoice_Email_Status__c= 'Reminder For Payment')
                                                                                    or (Ageing__c=1 And Status_Reference__c= 'Pending' And Invoice_Email_Status__c= 'Due Reminder')
                                                                                    or (Ageing__c=30 And Status_Reference__c= 'Pending' And Invoice_Email_Status__c= 'Extra Charges Email')
                                                                                    or (Ageing__c=45 And Status_Reference__c='Pending' And Invoice_Email_Status__c= 'Account Cancel')]);
        }

        global void execute(Database.BatchableContext BC,list<Invoice__c > scope) {

                List<Invoice__c> InvUpdateList = New List<Invoice__c>();
                List<EmailTemplate> etemp=[Select Id,Name,HtmlValue, Body,Subject  From EmailTemplate where name='Invoice Email' limit 1 ];
                List<EmailTemplate> etemps=[Select Id,Name,HtmlValue, Body,Subject  From EmailTemplate where name='Pending Invoice Email' limit 1 ];
                List<EmailTemplate> etempnuk=[Select Id,Name,HtmlValue, Body,Subject  From EmailTemplate where name='NON_UK_Invoice_Email_Templete' limit 1 ];
                List<EmailTemplate> etemptech=[Select Id,Name,HtmlValue, Body,Subject  From EmailTemplate where name='UK Tech Invoice Template' limit 1 ];
                List<EmailTemplate> etemprgtech=[Select Id,Name,HtmlValue, Body,Subject  From EmailTemplate where name='RG Tech Invoice Template' limit 1 ];

        for(Invoice__c  inv:scope) {

             If(inv.Ageing__c==-26 && inv.Status_Reference__c== 'Raised' && inv.Invoice_Email_Status__c== 'Invoice Ok To Send') {
                    <b>if(inv.Invoice_Billing_Entity__c=='RG India'){
                        // Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
                        **Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                        system.debug('-----Email---=' +etemp);
                        system.debug('-----Email---=' +ListInv);
                        system.debug('-----Email---=' +Invsend);
                        //mail.setTemplateId('etemp');
                        //mail.setTemplateId('00XO0000000Lygn');
                        mail.setTemplateId(etemp[0].id);
                        mail.setTargetObjectId(inv.Contact_Person__c);
                        mail.setSenderDisplayName('Account Receivable');
                        mail.setUseSignature(false);
                        mail.setReplyTo('abhinav.shukla@rategain.com');
                        mail.setBccSender(false);
                        mail.setSaveAsActivity(True);
                        mail.setWhatId(inv.id);
                        Messaging.SendEmailResult [] r = 
                        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});** 
                        system.debug('-----Email---=' +r);  
                        system.debug('-----RG India---=' +inv.Invoice_Billing_Entity__c);    

                    }else if(inv.Invoice_Billing_Entity__c=='RG UK'){
                        // Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
                        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                        system.debug('-----Email---=' +etemp);
                        system.debug('-----Email---=' +ListInv);
                        system.debug('-----Email---=' +Invsend);
                        //mail.setTemplateId('etemp');
                        //mail.setTemplateId('00XO0000000Lygn');
                        mail.setTemplateId(etempnuk[0].id);
                        mail.setTargetObjectId(inv.Contact_Person__c);
                        mail.setSenderDisplayName('Account Receivable');
                        mail.setUseSignature(false);
                        mail.setReplyTo('abhinav.shukla@rategain.com');
                        mail.setBccSender(false);
                        mail.setSaveAsActivity(True);
                        mail.setWhatId(inv.id);
                        Messaging.SendEmailResult [] r = 
                        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail}); 
                        system.debug('-----Email---=' +r);    
                        system.debug('-----RG UK---=' +inv.Invoice_Billing_Entity__c); 

                    }else if(inv.Invoice_Billing_Entity__c=='RG Tech'){
                        // Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
                        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                        system.debug('-----Email---=' +etemp);
                        system.debug('-----Email---=' +ListInv);
                        system.debug('-----Email---=' +Invsend);
                        //mail.setTemplateId('etemp');
                        //mail.setTemplateId('00XO0000000Lygn');
                        mail.setTemplateId(etemprgtech[0].id);
                        mail.setTargetObjectId(inv.Contact_Person__c);
                        mail.setSenderDisplayName('Account Receivable');
                        mail.setUseSignature(false);
                        mail.setReplyTo('abhinav.shukla@rategain.com');
                        mail.setBccSender(false);
                        mail.setSaveAsActivity(True);
                        mail.setWhatId(inv.id);
                        Messaging.SendEmailResult [] r = 
                        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail}); 
                        system.debug('-----Email---=' +r);   
                        system.debug('-----RG Tech---=' +inv.Invoice_Billing_Entity__c);  

                    }else{
                        // Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
                        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                        system.debug('-----Email---=' +etemp);
                        system.debug('-----Email---=' +ListInv);
                        system.debug('-----Email---=' +Invsend);
                        //mail.setTemplateId('etemp');
                        //mail.setTemplateId('00XO0000000Lygn');
                        mail.setTemplateId(etemptech[0].id);
                        mail.setTargetObjectId(inv.Contact_Person__c);
                        mail.setSenderDisplayName('Account Receivable');
                        mail.setUseSignature(false);
                        mail.setReplyTo('abhinav.shukla@rategain.com');
                        mail.setBccSender(false);
                        mail.setSaveAsActivity(True);
                        mail.setWhatId(inv.id);
                        Messaging.SendEmailResult [] r = 
                        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail}); 
                        system.debug('-----Email---=' +r);  
                        system.debug('-----UK Tech---=' +inv.Invoice_Billing_Entity__c);   
                    }
                        inv.Invoice_Email_Status__c='Reminder For Payment';
                        InvUpdateList.add(inv);
                        system.debug('-----Invoice Update Reminder For Payment---=' +InvUpdateList);

            }</b>

        else if (inv.Ageing__c==-12 && inv.Status_Reference__c== 'Raised' && inv.Invoice_Email_Status__c== 'Reminder For Payment'){

                //  ListInv1.add(inv.id);
                //  Invrem.add(inv.Contact_Person__c);
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                system.debug('-----Email---=' +etemp);
                system.debug('-----Email---=' +ListInv);
                system.debug('-----Email---=' +Invsend);
                //mail.setTemplateId('etemp');
                //mail.setTemplateId('00XO0000000Lygn');
                mail.setTemplateId(etemps[0].id);
                mail.setTargetObjectId(inv.Contact_Person__c);
                mail.setSenderDisplayName('Account Receivable');
                mail.setUseSignature(false);
                mail.setReplyTo('abhinav.shukla@rategain.com');
                mail.setBccSender(false);
                mail.setSaveAsActivity(True);
                mail.setWhatId(inv.id);
                Messaging.SendEmailResult [] r = 
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail}); 
                system.debug('-----Email---=' +r);   
                inv.Invoice_Email_Status__c='Due Reminder'; 
                InvUpdateList.add(inv);
                system.debug('-----Invoice Update Due Reminder---=' +InvUpdateList);

        }

        else if (inv.Ageing__c==1 && inv.Status_Reference__c== 'Pending' && inv.Invoice_Email_Status__c== 'Due Reminder'){

                //  ListInv1.add(inv.id);
                //  Invrem.add(inv.Contact_Person__c);
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                system.debug('-----Email---=' +etemp);
                system.debug('-----Email---=' +ListInv);
                system.debug('-----Email---=' +Invsend);
                //mail.setTemplateId('etemp');
                //mail.setTemplateId('00XO0000000Lygn');
                mail.setTemplateId(etemps[0].id);
                mail.setTargetObjectId(inv.Contact_Person__c);
                mail.setSenderDisplayName('Account Receivable');
                mail.setUseSignature(false);
                mail.setReplyTo('abhinav.shukla@rategain.com');
                mail.setBccSender(false);
                mail.setSaveAsActivity(True);
                mail.setWhatId(inv.id);
                Messaging.SendEmailResult [] r = 
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail}); 
                system.debug('-----Email---=' +r);    
                inv.Invoice_Email_Status__c='Extra Charges Email'; 
                InvUpdateList.add(inv);
                system.debug('-----Invoice Update Extra Charges Email---=' +InvUpdateList);

        }

        else if (inv.Ageing__c==30 && inv.Status_Reference__c== 'Pending' && inv.Invoice_Email_Status__c== 'Extra Charges Email'){

                //  ListInv1.add(inv.id);
                //  Invrem.add(inv.Contact_Person__c);
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                system.debug('-----Email---=' +etemp);
                system.debug('-----Email---=' +ListInv);
                system.debug('-----Email---=' +Invsend);
                //mail.setTemplateId('etemp');
                //mail.setTemplateId('00XO0000000Lygn');
                mail.setTemplateId(etemps[0].id);
                mail.setTargetObjectId(inv.Contact_Person__c);
                mail.setSenderDisplayName('Account Receivable');
                mail.setUseSignature(false);
                mail.setReplyTo('abhinav.shukla@rategain.com');
                mail.setBccSender(false);
                mail.setSaveAsActivity(True);
                mail.setWhatId(inv.id);
                Messaging.SendEmailResult [] r = 
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail}); 
                system.debug('-----Email---=' +r);   
                inv.Invoice_Email_Status__c='Account Cancel'; 
                InvUpdateList.add(inv);
                system.debug('-----Invoice Update Account Cancel---=' +InvUpdateList);

        }

        else if (inv.Ageing__c==45 && inv.Status_Reference__c== 'Pending' && inv.Invoice_Email_Status__c== 'Account Cancel'){

                //  ListInv1.add(inv.id);
                //  Invrem.add(inv.Contact_Person__c);
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                system.debug('-----Email---=' +etemp);
                system.debug('-----Email---=' +ListInv);
                system.debug('-----Email---=' +Invsend);
                //mail.setTemplateId('etemp');
                //mail.setTemplateId('00XO0000000Lygn');
                mail.setTemplateId(etemps[0].id);
                mail.setTargetObjectId(inv.Contact_Person__c);
                mail.setSenderDisplayName('Account Receivable');
                mail.setUseSignature(false);
                mail.setReplyTo('abhinav.shukla@rategain.com');
                mail.setBccSender(false);
                mail.setSaveAsActivity(True);
                mail.setWhatId(inv.id);
                Messaging.SendEmailResult [] r = 
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail}); 
                system.debug('-----Email---=' +r);    

        }

        }
        if(InvUpdateList.size()>0){
        update InvUpdateList;
        system.debug('-----Invoice Update---=' +InvUpdateList);
        }
        system.debug('-----Invoice Update---=' +InvUpdateList);

        }

        global void finish(Database.BatchableContext BC) {

        }

        }

```

==================================**Test Class**========================================

```
@isTest
  private class Test_InvoiceAutomationClass1 { 
      private Messaging.SingleEmailMessage[] sent; 
static testMethod void InvoiceAutomationClass1_TestMethod (){
     List<Invoice__c> invList = new List<Invoice__c>(); 
      Messaging.Singleemailmessage testEmail;
      List<Messaging.Sendemailresult> testEmailResults;

     Profile prof = [select id from profile where name='system Administrator'];
     User usr = new User(alias = 'usr', email='us.name@vmail.com',
                emailencodingkey='UTF-8', lastname='lstname',
                timezonesidkey='America/Los_Angeles',
                languagelocalekey='en_US',
                localesidkey='en_US', profileid = prof.Id,
                username='testuser128@testorg.com');
                insert usr;
   Account acc = new Account(name='testName', Ownerid = usr.id);
   insert acc ;

        Contract Contr=new Contract();
            Contr.AccountId=acc.Id;
            Contr.Status='Draft';
            Insert Contr;

            Contact con = new Contact(Email='vivek.istarmca@gmail.com',LastName='shukla',FirstName='Abhinav');
            insert con;

        for(integer i = 0; i < 200; i++){
        Invoice__c Invc=new Invoice__c();
            Invc.Contract__c=Contr.id;
            Invc.Vertical__c='PS';
            Invc.InvoiceDate__c=system.today();
            Invc.Credit_Days__c=10;
            Invc.Currency__c='USD';
            Invc.Term__c='Monthly';
            Invc.Invoice_Email_Status__c= 'Reminder For Payment';
            Invc.Conversion_Rate_To_INR__c=100;
            Invc.Contact_Person__c= con.id;
            Invc.Invoice_Billing_Entity__c= 'RG UK';
            Invc.Conversion_Rate_To_USD__c=1.000000;
            Invc.Type__c='Fresh';
            Invc.Property_Name__c='TestMon';
            Invc.From__c=system.today()-20;
            Invc.To__c=system.today();
            invList.add(Invc);

            }
            insert invList;
             List<EmailTemplate> etemp=[Select Id,Name,HtmlValue, Body,Subject  From EmailTemplate where name='Invoice Email' limit 1 ];

              testEmail = new Messaging.SingleEmailMessage();

                        //mail.setTemplateId('etemp');
                        //mail.setTemplateId('00XO0000000Lygn');
                        testEmail.setTemplateId(etemp[0].id);
                        testEmail.setTargetObjectId(invList[0].Contact_Person__c);
                        testEmail.setSenderDisplayName('Account Receivable');
                        testEmail.setUseSignature(false);
                        testEmail.setReplyTo('abhinav.shukla@rategain.com');
                        testEmail.setBccSender(false);
                        testEmail.setSaveAsActivity(True);
                        testEmail.setWhatId(invList[0].id);

            testEmailResults = 
                        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {testEmail}); 

   Test.StartTest();
        CallingInvoiceAutomationClass1 p = new CallingInvoiceAutomationClass1();
            String sch = '0 0 8 13 2 ?';
            system.schedule('One Time Pro', sch, p);

        InvoiceAutomationClass1 objBatch = new InvoiceAutomationClass1();
            Integer batchSize = 10;  
            Database.QueryLocator ql = objBatch.start(null);
            objBatch.execute(null,invList);
            objBatch.Finish(null);
            ID batchprocessid = Database.executeBatch(objBatch,batchSize );
            Integer emailbefore = Limits.getEmailInvocations();
            System.assertEquals(emailbefore, Limits.getEmailInvocations());
    Test.StopTest();

  }

}

```

**Updated Test Class but still get 17% coverage**=========================================================================

I have change the cod like below for test class but still its shows 17% coverage in batch class. kindly suggest...!! Thanks

```
  @isTest 
  private class Test_InvoiceAutomationClass1 { 
      private Messaging.SingleEmailMessage[] sent; 
      static testMethod void InvoiceAutomationClass1_TestMethod (){
      List<Invoice__c> invList = new List<Invoice__c>(); 
      Messaging.Singleemailmessage testEmail;
      List<Messaging.Sendemailresult> testEmailResults;

     Profile prof = [select id from profile where name='system Administrator'];
     User usr = new User(alias = 'usr', email='us.name@vmail.com',
                emailencodingkey='UTF-8', lastname='lstname',
                timezonesidkey='America/Los_Angeles',
                languagelocalekey='en_US',
                localesidkey='en_US', profileid = prof.Id,
                username='testuser128@testorg.com');
                insert usr;
   Account acc = new Account(name='testName', Ownerid = usr.id);
   insert acc ;       

            Contract Contr=new Contract();
            Contr.AccountId=acc.Id;
            Contr.Status='Draft';
            Insert Contr;

            Contact con = new Contact(Email='vivek.istarmca@gmail.com',LastName='shukla',FirstName='Abhinav');
            insert con;

            Invoice__c Invc=new Invoice__c();
            Invc.Contract__c=Contr.id;            
            Invc.Vertical__c='PS';
            Invc.InvoiceDate__c = system.today();
            Invc.Invoice_Due_Date__c = date.newinstance(2015, 5, 13);
            //Invc.Invoice_Due_Date__c = date.today() + 26;
            Invc.Invoice_Status__c  = 'Raised';
            Invc.Invoice_Billing_Entity__c = 'RG India';
            Invc.Credit_Days__c=10;
            Invc.Currency__c='USD';
            Invc.Term__c='Monthly';
            Invc.Invoice_Email_Status__c= 'Invoice Ok To Send';
            Invc.Conversion_Rate_To_INR__c=100;
            Invc.Contact_Person__c= con.id;
            Invc.Conversion_Rate_To_USD__c=1.000000;
            Invc.Type__c='Fresh';
            Invc.Property_Name__c='TestMon';
            Invc.From__c=system.today()-20;
            Invc.To__c=system.today();
            invList.add(Invc);            

           Invoice__c Invc1=new Invoice__c();
            Invc1.Contract__c=Contr.id;            
            Invc1.Vertical__c='PS';
            Invc1.InvoiceDate__c = system.today();
            Invc1.Invoice_Due_Date__c = date.newinstance(2015, 5, 13);
            //Invc1.Invoice_Due_Date__c = date.today() + 26;
            Invc1.Invoice_Status__c  = 'Raised';
            Invc1.Credit_Days__c=10;
            Invc1.Currency__c='USD';
            Invc1.Term__c='Monthly';
            Invc1.Invoice_Email_Status__c= 'Invoice Ok To Send';
            Invc1.Conversion_Rate_To_INR__c=100;
            Invc1.Contact_Person__c= con.id;
            Invc1.Invoice_Billing_Entity__c= 'RG UK';
            Invc1.Conversion_Rate_To_USD__c=1.000000;
            Invc1.Type__c='Fresh';
            Invc1.Property_Name__c='TestMon';
            Invc1.From__c=system.today()-20;
            Invc1.To__c=system.today();
            invList.add(Invc1);  

             Invoice__c Invc2=new Invoice__c();
            Invc2.Contract__c=Contr.id;            
            Invc2.Vertical__c='PS';
            Invc2.InvoiceDate__c = system.today();
            Invc2.Invoice_Due_Date__c = date.newinstance(2015, 5, 13);
            //Invc2.Invoice_Due_Date__c = date.today() + 26;
            Invc2.Invoice_Status__c  = 'Raised';
            Invc2.Credit_Days__c=10;
            Invc2.Currency__c='USD';
            Invc2.Term__c='Monthly';
            Invc2.Invoice_Email_Status__c= 'Invoice Ok To Send';
            Invc2.Conversion_Rate_To_INR__c=100;
            Invc2.Contact_Person__c= con.id;
            Invc2.Invoice_Billing_Entity__c= 'RG Tech';
            Invc2.Conversion_Rate_To_USD__c=1.000000;
            Invc2.Type__c='Fresh';
            Invc2.Property_Name__c='TestMon';
            Invc2.From__c=system.today()-20;
            Invc2.To__c=system.today();
            invList.add(Invc2);  

             Invoice__c Invc3=new Invoice__c();
            Invc3.Contract__c=Contr.id;            
            Invc3.Vertical__c='PS';
            Invc3.InvoiceDate__c = system.today();
            Invc3.Invoice_Due_Date__c = date.newinstance(2015, 5, 13);
            //Invc3.Invoice_Due_Date__c = date.today() + 26;
            Invc3.Invoice_Status__c  = 'Raised';
            Invc3.Credit_Days__c=10;
            Invc3.Currency__c='USD';
            Invc3.Term__c='Monthly';
            Invc3.Invoice_Email_Status__c= 'Invoice Ok To Send';
            Invc3.Conversion_Rate_To_INR__c=100;
            Invc3.Contact_Person__c= con.id;
            Invc3.Invoice_Billing_Entity__c= 'UK Tech';
            Invc3.Conversion_Rate_To_USD__c=1.000000;
            Invc3.Type__c='Fresh';
            Invc3.Property_Name__c='TestMon';
            Invc3.From__c=system.today()-20;
            Invc3.To__c=system.today();
            invList.add(Invc3);  

            insert invList;

             List<EmailTemplate> etemp=[Select Id,Name,HtmlValue, Body,Subject  From EmailTemplate where name='Invoice Email' limit 1 ];

              testEmail = new Messaging.SingleEmailMessage();

                        //mail.setTemplateId('etemp');
                        //mail.setTemplateId('00XO0000000Lygn');
                        testEmail.setTemplateId(etemp[0].id);
                        testEmail.setTargetObjectId(invList[0].Contact_Person__c);
                        testEmail.setSenderDisplayName('Account Receivable');
                        testEmail.setUseSignature(false);
                        testEmail.setReplyTo('abhinav.shukla@rategain.com');
                        testEmail.setBccSender(false);
                        testEmail.setSaveAsActivity(True);
                        testEmail.setWhatId(invList[0].id);

            testEmailResults = 
                        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {testEmail}); 

   Test.StartTest();
        CallingInvoiceAutomationClass1 p = new CallingInvoiceAutomationClass1();
            String sch = '0 0 8 13 2 ?';
            system.schedule('One Time Pro', sch, p);

        InvoiceAutomationClass1 objBatch = new InvoiceAutomationClass1();
            Integer batchSize = 10;  
            Database.QueryLocator ql = objBatch.start(null);
            objBatch.execute(null,invList);
            objBatch.Finish(null);
            ID batchprocessid = Database.executeBatch(objBatch,batchSize );
            Integer emailbefore = Limits.getEmailInvocations();
            System.assertEquals(emailbefore, Limits.getEmailInvocations());
    Test.StopTest();

  }

}

```