+ Start a Discussion
Robert Lange 6Robert Lange 6 

Trying to write test class for trigger that adds Opportunity on Account object

I have written this trigger which adds and opportunity to any new account insertions and account updates (where the account does not have an existing related opportunity).  My current test class provides 76% coverage.  Any advice would be appreciated!

APEX TRIGGER:
trigger AddRelatedRecord on Account(after insert, after update) {

    if (Trigger.isInsert) {
        for (Account a : Trigger.new) {
            Opportunity opp = new Opportunity();
            opp.Name = a.Name + ' Opportunity';
            opp.StageName = 'Prospecting';
            opp.CloseDate = System.today().addMonths(1);
            opp.AccountId = a.Id;
            insert opp;

        }
    }

    if (Trigger.isUpdate) {
        List<Opportunity> oppList = new List<Opportunity>();

        List<Account> accountsWithoutOppsAndGotUpdated = [
                SELECT Id, Name
                FROM Account
                WHERE Id NOT IN (SELECT AccountId FROM Opportunity) AND Id IN :Trigger.new];

 
        for (Account a : accountsWithoutOppsAndGotUpdated) {
            oppList.add(new Opportunity(Name = a.Name + ' Opportunity',
                    StageName = 'Prospecting',
                    CloseDate = System.today().addMonths(1),
                    AccountId = a.Id));
        }
        insert oppList;
    }
}

HERE IS MY CURRENT TEST CLASS:
@IsTest
private class AddRelatedRecordTest {

    @IsTest public static void addRelatedRecordTestonInsert() {

        Account testAcc = new Account(Name = 'My Account Test');
        insert testAcc;
        update testAcc;
        
        System.assert(true, 'Account Updated');

    }

}
Best Answer chosen by Robert Lange 6
TechingCrewMattTechingCrewMatt
Hi, Robert. I'm guessing the update portion of the trigger is not being covered. The update functionality of the trigger will only apply to existing Accounts without Opportunities. I would add another test method that test the update. It will look something like the following:
@IsTest
private class AddRelatedRecordTest {

    @IsTest public static void addRelatedRecordTestOnUpdate() {

        Account testAcc = new Account(Name = 'My Account Test');
        insert testAcc;
        //delete the Opp created by the Account insert
        delete [SELECT Id FROM Opportunity];
        testAcc.Name = 'Updated Account';
        
        Test.startTest();
        update testAcc;
        Test.stopTest();
        
        List<Opportunity> oppsAfterUpdate = new List<Opportunity>([
             SELECT Id FROM Opportunity WHERE Account.Name = 'Updated Account'
        ]);
        System.assertNotEquals(0, oppsAfterUpdate.size(), 'An Opp should have been inserted.');
    }

}

 

All Answers

TechingCrewMattTechingCrewMatt
Hi, Robert. I'm guessing the update portion of the trigger is not being covered. The update functionality of the trigger will only apply to existing Accounts without Opportunities. I would add another test method that test the update. It will look something like the following:
@IsTest
private class AddRelatedRecordTest {

    @IsTest public static void addRelatedRecordTestOnUpdate() {

        Account testAcc = new Account(Name = 'My Account Test');
        insert testAcc;
        //delete the Opp created by the Account insert
        delete [SELECT Id FROM Opportunity];
        testAcc.Name = 'Updated Account';
        
        Test.startTest();
        update testAcc;
        Test.stopTest();
        
        List<Opportunity> oppsAfterUpdate = new List<Opportunity>([
             SELECT Id FROM Opportunity WHERE Account.Name = 'Updated Account'
        ]);
        System.assertNotEquals(0, oppsAfterUpdate.size(), 'An Opp should have been inserted.');
    }

}

 
This was selected as the best answer
Robert Lange 6Robert Lange 6
Matt, thank you very much.  What is the significance of 'An Opp should have been inserted'.  I'm kind of a newbie, and I haven't seen something written in quotes in a System.assertNotEquals before.  I understand how the first two items are both false statements.
TechingCrewMattTechingCrewMatt
Robert, the System.assertNotEquals() method takes an option third argument which is a friendly message. If the assertion fails, instead of providing a generic exception message, it will show the friendly message. This is helpful when a test method fails upon deployment and you're trying to figure where and why the error was thrown.

I always recommend learning from the official Salesforce Apex Developer Guide Class documents:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_system.htm
 
Tec GyanTec Gyan
Thanks bro