You need to sign in to do that
Don't have an account?
Stuck on Test Class. Not sure how to get code covered.
I'm having trouble figuring out what needs to be done to test a trigger. I've looked at any tutorials/examples I could find and tried the concepts but I'm just not getting any code coverage from what I've been doing.
Would really appreciate some guidance here... there's something I'm not understanding.
Thanks in advance!
Overview:
Switching to a new data model where different category items are now stored in 1 object instead of across multiple objects.
So the trigger checks if 11 CategoryItems exist for a certain building, and adds them if they don't.
I can't figure out how to get coverage for all the 'categoryItems.add' statements. Best way I could think of was to do the IF statement and assertEquals... But that's not working.
Here is the trigger:
trigger addCategoryItemsToOldData on Building__c (after update) { for(Building__c b : trigger.new) { List<CategoryItem__c> cats = [SELECT Id, categoryType__c, Name FROM CategoryItem__c WHERE building__c = :b.Id]; if( cats.size() == 0 ) { List<CategoryItem__c> categoryItems = new List<CategoryItem__c>{}; //1.1 This is for the new data model categoryItems.add(new CategoryItem__c(Building__c = b.id, categoryType__c = 'Cat1', Name = 'Cat1')); categoryItems.add(new CategoryItem__c(Building__c = b.id, categoryType__c = 'Cat2', Name = 'Cat2')); categoryItems.add(new CategoryItem__c(Building__c = b.id, categoryType__c = 'Cat3', Name = 'Cat3')); categoryItems.add(new CategoryItem__c(Building__c = b.id, categoryType__c = 'Cat4', Name = 'Cat4')); categoryItems.add(new CategoryItem__c(Building__c = b.id, categoryType__c = 'Cat5', Name = 'Cat5')); categoryItems.add(new CategoryItem__c(Building__c = b.id, categoryType__c = 'Cat6', Name = 'Cat6')); categoryItems.add(new CategoryItem__c(Building__c = b.id, categoryType__c = 'Cat7', Name = 'Cat7')); categoryItems.add(new CategoryItem__c(Building__c = b.id, categoryType__c = 'Cat8', Name = 'Cat8')); categoryItems.add(new CategoryItem__c(Building__c = b.id, categoryType__c = 'Cat9', Name = 'Cat9')); categoryItems.add(new CategoryItem__c(Building__c = b.id, categoryType__c = 'Cat10', Name = 'Cat10')); categoryItems.add(new CategoryItem__c(Building__c = b.id, categoryType__c = 'Cat11', Name = 'Cat11')); insert categoryItems; } } }
Here is my test Class:
@istest
public class testAddCategoryItemsToOldData
{
static testMethod void addCategoryItemsToOldDataModel()
{
List<Building__c> bldg = new List<Building__c>{};
List<Account> acct = new List<Account>{};
test.startTest();
for(Integer i = 0; i < 20; i++)
{
Account newAcct = new Account(Name = 'Test Account ABC00_' +i, County__c = 'TheCounty');
acct.add(newAcct);
}
insert acct;
for(Integer i = 0; i < 20; i++)
{
Building__c bldgs = new Building__c(Name = 'Test Building BL00_'+i, State__c = 'FL', Account__c = acct[i].Id, City__c = 'TheCity', Zip_Code__c = '99999');
bldg.add(bldgs);
insert bldgs;
//bldgs.Zip_Code__c = '01234';
//update bldgs;
}
//insert bldg;
/*
for(Building__c b : bldg)
{
//b.Zip_Code__c = '01234';
}
//update bldg;
*/
test.stopTest();
for(Building__c bb : bldg)
{
List<CategoryItem__c> cats = [SELECT Name, categoryType__c FROM CategoryItem__c WHERE Building__c = :bb.Id];
system.AssertEquals(11, cats.size());
for(CategoryItem__c c: cats)
{
system.Debug('*****************************************************************************************************************');
system.Debug('* * * * * * * * * * * * *' + c.Name);
system.Debug('*****************************************************************************************************************');
if(c.Name == 'cat1')
{
system.AssertEquals('cat1', c.Name);
}
if(c.Name == 'cat2')
{
system.AssertEquals('cat2', c.Name);
}
if(c.Name == 'cat3')
{
system.AssertEquals('cat3', c.Name);
}
if(c.Name == 'cat4')
{
system.AssertEquals('cat4', c.Name);
}
if(c.Name == 'cat5')
{
system.AssertEquals('cat5', c.Name);
}
if(c.Name == 'cat6')
{
system.AssertEquals('cat6', c.Name);
}
//Add in other IF's if code coverage increases...
}
}
}
}
Hi Wizno,
you just have to insert and update Building record and update it in your test coverage which are not having any CategoryItem.
All Answers
Hi Wizno,
you just have to insert and update Building record and update it in your test coverage which are not having any CategoryItem.
Here's what worked: