You need to sign in to do that
Don't have an account?

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);
}
}
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 :)
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 ?
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 ?
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 :)
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?
So you mean to say, if you want any other fields other than Id, you need to query it?
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.
Cool...thanks Oleg...!