This question already has an answer here:

I have written the following class and it functions properly. Unfortunately, I dont even know how to get started on writing the test class for this. Any guidance would be greatly appreciated. Here is the class:

public class ServiceReassignmentButtonCtrl {
    private final Dealership__c dealer;
    private final String percentPremiumEmailTemplate = 'Percent_Prm_Reassign';
    private final String ServiceReassignmentTemplate = 'Service_Reassignment_Template';
    private final String FCAReassignmentTemplate = 'FCA_Reassignment_Template';
    private final String ReassignmentEmailTemplate   = 'Reassignment_Template';
    private final String AudiOptin = 'AudiOptInStandard';
    private final String AudiOptinPrem = 'AudiOptInPercentPrem';
    private final String BMWOptin = 'BMWOptInStandard';
    private final String BMWOptinPrem = 'BMWOptInPercentPrem';

    transient public Account VisualForceAccount{get; set;}

    public ServiceReassignmentButtonCtrl(ApexPages.StandardController stdController){
        this.dealer = (Dealership__c)stdController.getRecord();

    public Dealership__c getDealerInfo(String dealerID){
        Dealership__c dealerInfo = [SELECT OwnerId, Dealership_Spiff_Payment__c, Partner__c FROM Dealership__c WHERE Id=:dealerID LIMIT 1];
        return dealerInfo;

    public List<Account> getClientList(String dealerID){
        List<Account> client = [SELECT Id, Participant_Name__c, Active__c FROM Account WHERE Dealership__c =:dealerID];

        return client;
    //Sends email
    public void autoRun(){
        String dealershipId = ApexPages.currentPage().getParameters().get('id');

        Dealership__c dealerInfo = getDealerInfo(dealershipId);
        String templateType;
        String optinForm;

        if(dealerInfo.Dealership_Spiff_Payment__c == '% premium'){
            templateType = percentPremiumEmailTemplate;
        }else if(dealerInfo.Partner__c.contains('Service')){
            templateType = ServiceReassignmentTemplate;
        }else if(dealerInfo.Partner__c.contains('Jeep') || dealerInfo.Partner__c.contains('RAM') || dealerInfo.Partner__c.contains('Chrysler')|| dealerInfo.Partner__c.contains('Dodge')|| dealerInfo.Partner__c.contains('Alfa Romeo') ||dealerInfo.Partner__c.contains('Fiat')){
            templateType = FCAReassignmentTemplate;

        }else {
            templateType = ReassignmentEmailTemplate;

        if (dealerInfo.Partner__c.contains('Audi')){
            optinForm = AudiOptin;  
        }else if(dealerInfo.Partner__c.contains('Audi') && dealerInfo.Dealership_Spiff_Payment__c == '% premium'){
            optinForm = AudiOptinPrem;
        }else if (dealerInfo.Partner__c.contains('BMW')){
            optinForm = BMWOptin;  
        }else (dealerInfo.Partner__c.contains('BMW') && dealerInfo.Dealership_Spiff_Payment__c == '% premium'){
            optinForm = BMWOptinPrem;

        EmailTemplate template = [Select id from EmailTemplate where DeveloperName =:templateType LIMIT 1];

        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 

        // Sets the paramaters of the email

        email.setTemplateId( template.id );
        email.setSaveAsActivity(false); // This is set as false while calling from apex

        List<Account> clientList = getClientList(dealershipId);

        Messaging.EmailFileAttachment[] attachmentList = new Messaging.EmailFileAttachment[]{};
        for(Account cl: clientList){
            if(cl.Active__c == 'Yes'){
                PageReference pdf = Page.attachmentPDF; 

                // Take the PDF content
                Blob b = pdf.getContent();

                // Create the email attachment
                Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
                efa.setFileName(cl.Participant_Name__c + '- Confirmation Card.pdf');

        if(optinForm!= null){
            StaticResource sr = [Select  s.Name, s.Id, s.Body From StaticResource s where s.Name =:optinForm];

            Blob tempBlob = sr.Body;
            Messaging.EmailFileAttachment opt = new Messaging.EmailFileAttachment();


        Messaging.SingleEmailMessage[] emailList = new Messaging.SingleEmailMessage[]{email};


Here is a shot at writing test class by googling but like I said it doesnt work and I have no clue what I am doing:

public class ServiceReassignmentButtonCtrl_Test {
    public static ServiceReassignmentButtonCtrl controller;
    static Account parentC;
    static Account client1;
    static Account client2;

    static testMethod void myCommonMethod() { 

        Dealership__c deal = new Dealership__c(Dealership_State__c = 'IN',Partner__c = 'BMW', Dealership_Spiff_Payment__c = '30');
        insert deal; 

        List<Account> listAcc = new List<account>();
        client1 = TestDataUtils.createAccountFromRecordType(Schema.SObjectType.Account.getRecordTypeInfosByName().get('Referral Program - Participant Form').getRecordTypeId());
        client1.Dealership__c = deal.ID;
        client1.Dealership_Partner__c = 'BMW';
        client1.Name = 'Parent Name';
        client1.Participant_Name__c = 'Afzaal';
        client1.Active__c = 'Yes';
        client1.Client_Number__c = '12345.0';
        //Account client1 = new Account(Dealership__c = deal.ID, Dealership_Partner__c = 'BMW', Client_Number__c = '12345', Participant_Name__c = 'Afzaal', Active__c='Yes');

        client2 = TestDataUtils.createAccountFromRecordType(Schema.SObjectType.Account.getRecordTypeInfosByName().get('Referral Program - Participant Form').getRecordTypeId());
        client2.Dealership__c = deal.ID;
        client2.Dealership_Partner__c = 'BMW';
        client2.Name = 'Parent Name2';
        client2.Participant_Name__c = 'Zanna';
        client2.Active__c = 'Yes';
        client2.Client_Number__c = '12346.0';
        //Account client2 = new Account(Dealership__c = deal.ID, Dealership_Partner__c = 'BMW', Client_Number__c = '12346', Participant_Name__c = 'Zanna', Active__c='Yes');

        insert listAcc;

        PageReference pageRef = Page.ServiceReassignmentButton;

        ApexPages.StandardController sc = new ApexPages.standardController(deal);

        ServiceReassignmentButtonCtrl controller = new ServiceReassignmentButtonCtrl(sc);



marked as duplicate by glls, battery.cord, Oleksandr Berehovskyi, Ratan Paul, Santanu Boral Dec 4 '17 at 23:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
  • I have written several test classes before. I think my confusion here is what do I do with a class that sends emails and attachments? How do I test that? What would by system.assert statements look like? Very lost and confused for some reason – user3688231 Nov 29 '17 at 15:07
  • I hope my answer on this post helps you out. – TSmith Nov 29 '17 at 15:20

If you care about what you are building in the email(s), you need two methods. One which does all the construction, and another to do the sending. Basically:

public static List<Messaging.SingleEmailMessage> buildEmails(/*inputs*/)
    // implementation
public static void sendEmails(/*inputs*/)

In this way, you can test that the emails are being sent, but also what they contain. Most of your test complexity would then be able to focus on the buildEmails method and checking that the output conforms to desired behavior. When testing sendEmails, you just need to assert against Limits.getEmailInvocations().

Not the answer you're looking for? Browse other questions tagged or ask your own question.