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
Jamal ArogundadeJamal Arogundade 

email test class - NEED HELP ON INCREASING THE CODE COVERAGE

The current code coverage is 25% 

BATCH CLASS

global class SendMembershipRenewalEmail30Days implements Database.Batchable<SObject>, schedulable {
    
    public Database.QueryLocator start(Database.BatchableContext bc){
        
        Date oneMonthFromToday  = Date.today().addDays(30); // Date 30 days from today
        return Database.getQueryLocator([select id, name, Organisation__r.npe01__one2onecontact__r.Email,regular_payment__c  FROM Membership__c WHERE Expiry_Date__c =: oneMonthFromToday and (regular_payment__r.npsp4hub__Payment_Method__c = 'cash' or regular_payment__r.npsp4hub__Payment_Method__c = 'Direct Debit' )]);
    }
    
    public void execute(Database.BatchableContext bc, list<Membership__c> scope){
        
        list <EmailTemplate> templateIds = new list <EmailTemplate> ([Select id from EmailTemplate where (name = 'Organisation Membership Renewals Direct Debit' OR name = 'Organisation Membership Renewals Not Direct Debit') ORDER BY name asc]);
        for(membership__c ta : (List<membership__c>)scope) {
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            email.setToAddresses(new String[] {ta.Organisation__r.npe01__one2onecontact__r.Email});
            email.setSaveAsActivity(false);
            email.setTargetObjectId(ta.OwnerId);
            email.setTemplateId(ta.regular_payment__c == null ? templateIds[1].id : templateIds[0].id);
            email.setWhatId(ta.Id);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        }
    }
    
    public void finish(Database.BatchableContext bc){
        
        
    }
    
    global void execute(SchedulableContext SC) {
        database.executebatch(new SendMembershipRenewalEmail30Days());
    }
}


TEST CLASS 

@isTest(seeAllData = false)
private class Test_SendMembershipRenewalEmail30Days {
    
    static testMethod void unitTest_SendMembershipRenewalEmail30Days()
    {
        Test.startTest();
        Account acc = new Account(name ='test name');
        insert acc;
        
        Contact cont = new Contact();
        cont.FirstName ='Test';
        cont.LastName ='Test';
        cont.email ='Test@Test.com';
        cont.accountid = acc.id;
        insert cont;
        
        Id oppRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Membership').getRecordTypeId();
        Opportunity opp = new Opportunity();
        opp.amount = 10.00;
        opp.Name = 'test name';
        opp.StageName = 'Closed Won';
        opp.RecordTypeId = oppRecordTypeId;
        opp.CloseDate = date.today();
        insert opp;
        
        
        
        
        
        SendMembershipRenewalEmail30Days obj = new SendMembershipRenewalEmail30Days();
        Database.executeBatch(obj);
        Test.stopTest();
        
    }
    
    
    
}
vishal-negandhivishal-negandhi

Hi Jamal, 


Your batch class is expecting "Membership__c" records and you haven't created any in your test class. 


If you've trigger on account / contact / opportunity, make sure to specify the right criteria so that you do have membership records that match the filter criteria in the start method. 

 

Hope this helps. 

Maharajan CMaharajan C
Hi Jamal,

Please follow the below steps:

1. Create the following records before the Membership__c:
        -> Organisation__C(May be account ) for lookup in  Membership__c
        -> npe01__one2onecontact__c with Email (May be Contact) for lookup in Organisation__C
        -> Update the npe01__one2onecontact__c field in Organisation__C record with above record created Id. 

2.  Create the Membership__c if it is Manually needs to be created otherwise check the automation logic for membership record creation and satisfy the criteria.

3.  Check the currect org having the email template  you are querying.

4.  Finally call the batch class.

Thanks,
Maharajan.C