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
JasiJasi 

Test class checking bulk help!

Hello guys,

 

I am writing a test class on a trigger and getting 100% code coverage. But I am not able to check the bulk trigger. Can anyone help me include bulk in this test class. Both my trigger and test class is given below. The trigger fetches the associated agency with the contest, and update the agency record with the contest id.

 

Trigger:

 

trigger Tgr_Contests_After on Contests__c (after insert, after update) {
    Map<ID, ID> mapContestAccount = new Map<ID, ID>();
        // get the information of the inserted contest records
        for(Contests__c contest:trigger.new){
            if(contest.agency__c != null){
                mapContestAccount.put(contest.agency__c, contest.id);               
            }
        }
    }
    // get the account information for the loaded contest records
    List<Account> lstAccount = [select contests__c from Account where id in: mapContestAccount.keySet() limit 10000];
    List<Account> lstAccToBeUpdated = new List<Account>();
    // iterate the accounts for updating the contest lookup
    for(Account acc : lstAccount){
        acc.Contests__c = mapContestAccount.get(acc.id);
        lstAccToBeUpdated.add(acc);
    }
    // check if the lstAccToBeUpdated is not empty
    if(!lstAccToBeUpdated.isEmpty()){
        update lstAccToBeUpdated;
    }
}

 

Test class :

 

@isTest(seeAllData=true)
private class Tgr_Contests_After_Test {

    static testMethod void myUnitTest() {
    
    Account acc=TestDataFactory.createtestaccount();  
    insert acc;    
acc= [Select Id from Account where Id = :acc.Id];

              // test data for contests
        Contests__c contest = TestDataFactory.createContest();
        contest.Agency__c = acc.id;

               insert contest;
acc= [Select Id,contests__c from Account where Id = :acc.Id];
contest= [Select Id,agency__c from Contests__c where Id = :contest.id];

        system.assertEquals(contest.id,acc.contests__c);
    }
}

Best Answer chosen by Admin (Salesforce Developers) 
__

In this case here is what I have... Let me know if it compiles ok, since I don't have a way of checking it :) 

 

 

@isTest
private class Tgr_Contests_After_Test {

  static testMethod void myUnitTest() {
    List <Account> listagency = new List <Account> ();
    List <Contests__c> listcontest = new List <Contests__c> ();
  
    for (integer i=0; i<200; i++){
      Account acc = new Account ( name='test'+i
                                  ,phone='1234'
                                  ,Physical_Street__c='Street'+i
                                  ,Physical_City__c='city'+i
                                  ,Physical_State_Province1__c='State'+i
                                  ,Physical_Zip_Postal_Code__c='1234'
                                  ,Agency_Status__c='Active');
      listagency.add(acc);
   }
   
   insert listagency;

   integer i=0;

    for (account accid : listagency){
      Contests__c contest = new Contests__c ( agency__c=accid.id
                                              ,Contests_Rank__c=1234+i
                                              ,Contests_Total__c=5678+i
                                              ,Contests_Date__c=date.today()+i
                                              ,Contests_ID__c ='T'+i);
      listcontest.add(contest);
      i++;
    }

    Test.starttest(); // not required
    insert listcontest;
    Test.stoptest();
     
    //when writing asserts ask yourself what would you do if you were testing it through UI? 
    // In this case you would probably go through accounts in the list and make sure contests__c lookup on the account is populated with a valid id

    List <Account> accList = [SELECT Id, Contests__c FROM Account WHERE Id IN :listagency];

    //you can cheat a bit and just check for nulls

    for (Account a: accList) {
      System.assertNotEquals (a.Contests__c, null, 'Contest is not populated on the account - ' + a.id);
    }

    // or you can get really fancy and create a map of account ids and contests to validate that a correct id is populated on each account

    Map <Id, Id> accountContestMap = new Map <Id, Id> ();
    for (Account a: accList) {
      accountContestMap.put(a.id, a.contests__c);
    }

    for (contests__c c: listcontest) {
      System.assertEquals (c.id, accountContestMap.get(c.agency__c), 'Lookup was incorrectly populated on Contest__c. Ids do not match.');
    }
  }
}

 

All Answers

__

You just need to create a bunch of accounts in a loop and bunch of contests and the insert accounts and insert contests. Small note - no need to limit query to 10000, there will never be more than 200 accounts returned. Also, best practice is to have seealldata = false. :) Does this help ?

JasiJasi

Hi Olegforce,

 

Thanks for your reply. I took your suggestion and wrote the following test class which is working , but I don't know how to write the assert statement now, to check if the trigger is working or not. I keep on getting errors...Can you please guide me..:(

 

@isTest
private class Tgr_Contests_After_Test {

    static testMethod void myUnitTest() {
    list<account> listagency = new list<account>();
    list<contests__c> listcontest=new list<contests__C>();
  
    for (integer i=0;i<200;i++)
    {
    Account acc=New Account(name='test'+i,phone='1234',Physical_Street__c='Street'+i,Physical_City__c='city'+i,Physical_State_Province1__c='State'+i,Physical_Zip_Postal_Code__c='1234',Agency_Status__c='Active');
   listagency.add(acc);
   }
   insert listagency;
   List <account> accids = new list <account> ([Select id from account where id In : listagency]);
   integer i=0;
   for (account accid : accids)
   {
   Contests__c contest=New Contests__c(agency__c=accid.id,Contests_Rank__c=1234+i,Contests_Total__c=5678+i,Contests_Date__c =date.today()+i,Contests_ID__c ='T'+i);
   listcontest.add(contest);
   i++;
   }
   Test.starttest();
   insert listcontest;
   Test.stoptest();
   
 
    }
}

__

Could you confirm that you are trying to create a 1:1 relationship between contest and account? From what I understand when a new contest is created you take account Ids from those records and populate contest lookup on the account. Is that correct ? 

JasiJasi

Yes., there is a lookup relationship between them. You are correct.

__

In this case here is what I have... Let me know if it compiles ok, since I don't have a way of checking it :) 

 

 

@isTest
private class Tgr_Contests_After_Test {

  static testMethod void myUnitTest() {
    List <Account> listagency = new List <Account> ();
    List <Contests__c> listcontest = new List <Contests__c> ();
  
    for (integer i=0; i<200; i++){
      Account acc = new Account ( name='test'+i
                                  ,phone='1234'
                                  ,Physical_Street__c='Street'+i
                                  ,Physical_City__c='city'+i
                                  ,Physical_State_Province1__c='State'+i
                                  ,Physical_Zip_Postal_Code__c='1234'
                                  ,Agency_Status__c='Active');
      listagency.add(acc);
   }
   
   insert listagency;

   integer i=0;

    for (account accid : listagency){
      Contests__c contest = new Contests__c ( agency__c=accid.id
                                              ,Contests_Rank__c=1234+i
                                              ,Contests_Total__c=5678+i
                                              ,Contests_Date__c=date.today()+i
                                              ,Contests_ID__c ='T'+i);
      listcontest.add(contest);
      i++;
    }

    Test.starttest(); // not required
    insert listcontest;
    Test.stoptest();
     
    //when writing asserts ask yourself what would you do if you were testing it through UI? 
    // In this case you would probably go through accounts in the list and make sure contests__c lookup on the account is populated with a valid id

    List <Account> accList = [SELECT Id, Contests__c FROM Account WHERE Id IN :listagency];

    //you can cheat a bit and just check for nulls

    for (Account a: accList) {
      System.assertNotEquals (a.Contests__c, null, 'Contest is not populated on the account - ' + a.id);
    }

    // or you can get really fancy and create a map of account ids and contests to validate that a correct id is populated on each account

    Map <Id, Id> accountContestMap = new Map <Id, Id> ();
    for (Account a: accList) {
      accountContestMap.put(a.id, a.contests__c);
    }

    for (contests__c c: listcontest) {
      System.assertEquals (c.id, accountContestMap.get(c.agency__c), 'Lookup was incorrectly populated on Contest__c. Ids do not match.');
    }
  }
}

 

This was selected as the best answer
JasiJasi

Great!! It worked...Thank you so much Oleg.

 

Now I have some doubts....when do you query actually? for example you created a new list - acclist - where you put all the account ids after querying because I believe you have to use id and contests__c later in the code. But you didn't do the same, I mean you didn't query for id and agency__c from contests__c , and you directly used listcontest. I am confused......pls help me understand !!

 

Thanks again..

Jasi

__

I had to query accList because contest was populated after trigger ran. so any lists I had would not have had contest ids. Now with the other list, you populated agency__c ids in the loop, added it to list  and inserted. In that case everything that you had in the list stayed, plus list now had contest ids. One thing to remember is that the only thing that gets updated in the list is a record id after inserting objects. the rest needs to be queried if it was not referenced earlier when you added objects or if there were workflows... otherwise it will be null. 

 

Does this make sense? 

JasiJasi

So you mean to say, if you want any other fields other than Id, you need to query it?

__
yes, unless you SET them when created a list. so if you did

list alist = new list ();

alist.add(new account (name='test', shippingstreet='street));
insert alist;

now if you do a list[0].name you will get name=test and street=street, but if a workflow updated street to street1 you will not see street1 unless you query.
JasiJasi

Cool...thanks Oleg...!