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
sapnasolankisapnasolanki 

Test Class Coverage for Trigger

This my Trigger code. What should the test class be for this for coverage?

 

trigger closedWonOppt on Opportunity (after Update)
{
    Opportunity opp = trigger.new[0];
    String oppId = opp.id;
    System.debug('oppId'+oppId);
    System.debug('in trigger');
    if(opp.StageName == 'Closed Won')
    {
        System.debug('in if');
        ACL__c acl = new ACL__c();
        Datetime today = datetime.now();
        Integer year = today.year();
        
        Opportunity opp1=[SELECT Name, Account.Name,Account.Id, RecordType.Name FROM Opportunity 
        where id = :oppId];
         
        acl.Name = opp1.Account.Name + ' - ' + opp1.RecordType.Name + ' - ' + String.valueOf(year);
        acl.Opportunity__c = opp1.Id;
        acl.account__C = opp1.Account.Id;
        // acl.Opportunity__c = opp1.Id;
         
        insert acl;
    }
}
Best Answer chosen by Admin (Salesforce Developers) 
OyeCodeOyeCode

//Typically test class is to test boundaries henceforth these should be accountable

// Create an Opportunity instance

// change the opportunity field (opp.stagename ='closed won' from the default)

// once you update and save (commited data will invoke this trigger)

// now we have to check to see if the updates have been made to related account fields

// Check 1 - pull up account for that oppotunities - then pull up field of Account (ac1.Name (system.Assert) ) check if not empty and if not the check the value you assigned

//Code below is the roughly estimate

 

@isTest

public class demoTest

{

static testMethod void test_Triggert()

{

Account acc = new Util.testAccount() ;            //best practise is to develop (utility class) and populate Account through it

insert acc;

Opportunity Opp = new Opportunity();          //create a new opportunity

oop.AccountId = acc.Id;                                  //relating opportunity with Account through Id match

//Assert Opportunity Stage first

System.assertEquals(opp.StageName ,'Open') ;   //assert if stage is open or system default that you chose to keep

// now change value to invoke trigger

Opp.StageName='Closed Won'                         

//Trigger should be invoked - Test the value that should be changed in Account (according to your trigger logic)

System.AssertEquals(acc.Name, opp.Account.Name);      // this should test that value is set through you logic

 

// this should end test at local level

 

// Step 2- Test for bulk updates (since production can encounter bulk data more than 1000 hence your test should cover bulk data both trigger logic and test logic

 

//Find how to write bulk trigger in my website and how write test class for bulk Trigger

 

Hope this helps at certain level

 

 

}

}

 

All Answers

OyeCodeOyeCode

//Typically test class is to test boundaries henceforth these should be accountable

// Create an Opportunity instance

// change the opportunity field (opp.stagename ='closed won' from the default)

// once you update and save (commited data will invoke this trigger)

// now we have to check to see if the updates have been made to related account fields

// Check 1 - pull up account for that oppotunities - then pull up field of Account (ac1.Name (system.Assert) ) check if not empty and if not the check the value you assigned

//Code below is the roughly estimate

 

@isTest

public class demoTest

{

static testMethod void test_Triggert()

{

Account acc = new Util.testAccount() ;            //best practise is to develop (utility class) and populate Account through it

insert acc;

Opportunity Opp = new Opportunity();          //create a new opportunity

oop.AccountId = acc.Id;                                  //relating opportunity with Account through Id match

//Assert Opportunity Stage first

System.assertEquals(opp.StageName ,'Open') ;   //assert if stage is open or system default that you chose to keep

// now change value to invoke trigger

Opp.StageName='Closed Won'                         

//Trigger should be invoked - Test the value that should be changed in Account (according to your trigger logic)

System.AssertEquals(acc.Name, opp.Account.Name);      // this should test that value is set through you logic

 

// this should end test at local level

 

// Step 2- Test for bulk updates (since production can encounter bulk data more than 1000 hence your test should cover bulk data both trigger logic and test logic

 

//Find how to write bulk trigger in my website and how write test class for bulk Trigger

 

Hope this helps at certain level

 

 

}

}

 

This was selected as the best answer
OyeCodeOyeCode

Find a code example I added here

 

Usually using for-loop when you create 1000 opportunities and try populating - you gradually have to make 1000 check and henceforth lots of SOQL calls. To test the bulk data, you need to avoid 100 + SOQL calls, so couple of gimmicks(tricks) i posted using standard practices in Salesforce Apex code

 

Check out one sample at http://www.forcelabs.net/2012/01/salesforce-trigger-to-count-contact.html

 

(This covers the logic of how to write trigger to handle bulk data (1000 +) and not reaching SOQL governer limit) smooth deploy in production

 

sapnasolankisapnasolanki

Thank you! that worked

OyeCodeOyeCode

Nice to hear that, I appreciate if you mark the my reply as solution.