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
Timothy SmithTimothy Smith 

Apex test for Class sending email

At a lost, I don't know where to start on the testing for this Class.  I don't know the next steps.  Any assistance moving on with the test for this class is appreciated.
 
public class caseTriggerHandlerCount{
    public static void CaseCounter(){

        List<AggregateResult> AggregateResultList = [SELECT AccountId, Account.Name name, COUNT(Id) co
                            FROM Case
                            WHERE CreatedDate = LAST_N_DAYS:50
                            GROUP BY AccountId, Account.Name
                            HAVING COUNT(Id)  >=1];

        for(AggregateResult aggr:AggregateResultList){ 
                
                    // Send Email to Implementation Coordinator
                Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                    message.toAddresses = new String[] { 'test@test.com' };   
                    message.subject = 'Subject Test Message';
                    message.plainTextBody = 'Account name: ' + aggr.get('name') + ' has ' + (Integer)aggr.get('co') + ' cases opened in the last 8 days.';
                Messaging.SingleEmailMessage[] messages =   new List<Messaging.SingleEmailMessage> {message};
                Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
            System.debug('Account Name: ' + aggr.get('name'));
            
            
        }

} 

}

I have created a Test Factory:
 
@isTest
public class TestDataFactoryCases {
    public static List<Case> createCasesData(Integer numConts) {
        List<Case> lConts = new List<Case>();
        
        for(Integer i=0;i<numConts;i++) {
            Case cse = new Case();
                    cse.status = 'Escalated';
                    cse.origin = 'Community';
                    cse.AccountId = '0013000000qLB9MAAW';
                    cse.Description = 'test ' + i;
            lConts.add(cse);
        }
        insert lConts;

        return lConts;
    }
}

My test Class So far:
 
@isTest
private class TestCaseCountAlert {
    @isTest static void TestPositiveResult() {
        // Test data setup
        
        Case[] cases = TestDataFactoryCases.createCasesData(10);
        
        // Perform test
        Test.startTest();
        
        Test.stopTest();
        
        
    }        
}

​​​​​​​
Best Answer chosen by Timothy Smith
Maharajan CMaharajan C

Hi Timothy,

Try the below changes in Test Factory and Test Class:

@isTest
public class TestDataFactoryCases {

    public static Account createAccountData() {
        Account acc = new Account();
        acc.Name = 'Test Account';
        // Please include if there is any other required fields needed to create Account
        return acc; 
    }

    
    public static List<Case> createCasesData(Integer numConts) {
        List<Case> lConts = new List<Case>();
        for(Integer i=0;i<numConts;i++) {
            Case cse = new Case();
                    cse.status = 'Escalated';
                    cse.origin = 'Community';
                    cse.Description = 'test ' + i;
                    // Please include if there is any other required fields needed to create Account
            lConts.add(cse);
        }
        return lConts;
    }
}

@isTest
private class TestCaseCountAlert {
    @isTest static void TestPositiveResult() {
        // Test data setup
        Account a = TestDataFactoryCases.createAccountData();
        insert a;
        

        Case[] cases = TestDataFactoryCases.createCasesData(10);
        for(case c : cases)
        {
            c.AccountId = a.Id;
        }

        insert cases;
        
        // Perform test
        Test.startTest();
            caseTriggerHandlerCount.CaseCounter();
        Test.stopTest();
        
    }        
}

Thanks,
Maharajan.C

All Answers

Rounak SharmaRounak Sharma
hello Timothy Smith,

Please refer the below code and modify it to your need

@istest

public class emailHelpertest{

    public static testmethod void testvalidate(){
    
        Account newAcc = new Account();
        newAcc.name='test';
        insert newAcc;
        
        Contact con=new Contact();
        con.lastname='Testing';
        con.email='test@test.com';
        insert con;
        
        
        emailHelper.sendEmail(con.Id,newAcc.Id );
    }
}
Please let me know if still you have any issues
thanks
Maharajan CMaharajan C

Hi Timothy,

Try the below changes in Test Factory and Test Class:

@isTest
public class TestDataFactoryCases {

    public static Account createAccountData() {
        Account acc = new Account();
        acc.Name = 'Test Account';
        // Please include if there is any other required fields needed to create Account
        return acc; 
    }

    
    public static List<Case> createCasesData(Integer numConts) {
        List<Case> lConts = new List<Case>();
        for(Integer i=0;i<numConts;i++) {
            Case cse = new Case();
                    cse.status = 'Escalated';
                    cse.origin = 'Community';
                    cse.Description = 'test ' + i;
                    // Please include if there is any other required fields needed to create Account
            lConts.add(cse);
        }
        return lConts;
    }
}

@isTest
private class TestCaseCountAlert {
    @isTest static void TestPositiveResult() {
        // Test data setup
        Account a = TestDataFactoryCases.createAccountData();
        insert a;
        

        Case[] cases = TestDataFactoryCases.createCasesData(10);
        for(case c : cases)
        {
            c.AccountId = a.Id;
        }

        insert cases;
        
        // Perform test
        Test.startTest();
            caseTriggerHandlerCount.CaseCounter();
        Test.stopTest();
        
    }        
}

Thanks,
Maharajan.C

This was selected as the best answer