You need to sign in to do that
Don't have an account?
Chad Moutes
Apex Test Class For My Tigger Not Working
I have an Apex Trigger listed below:
And I am trying to write a test class for it to get full code coverage, because my production org is currently at 70% so i need to bring the average up.
Can anyone give me any ideas?
trigger ActiveAssignedBDMsTrigger on Assigned_BDM__c (after insert, after update, after delete) { set<Id> set_id = new set<Id>(); List<Project__c>prj_list = new List<Project__c>(); if(trigger.isInsert || trigger.isUpdate) { for(Assigned_BDM__c bdm: trigger.new) { set_id.add(bdm.CampaignX__c); } } else if(trigger.isDelete) { for(Assigned_BDM__c bdm: trigger.old) { set_id.add(bdm.CampaignX__c); } } if(trigger.isAfter && (trigger.isUpdate || trigger.isInsert || trigger.isDelete)) { prj_list = [SELECT id, Number_Of_Active_BDMs__c, (SELECT id,name FROM Assigned_BDMs1__r WHERE Active__c = TRUE) FROM Project__c WHERE id IN :set_id]; for(Project__c prj: prj_list) { if(prj.Assigned_BDMs1__r.size() > 0) prj.Number_Of_Active_BDMs__c = prj.Assigned_BDMs1__r.size(); else prj.Number_Of_Active_BDMs__c = 0; } if(!prj_list.isEmpty()) update prj_list; } }
And I am trying to write a test class for it to get full code coverage, because my production org is currently at 70% so i need to bring the average up.
Can anyone give me any ideas?
Try this test class
@isTest
public class ActiveAssignedBDMsTriggerTest{
static testmethod void testInsert(){
Project__c proj = new Project__c(Name = 'Test'); //Add all the required fields
insert proj;
Assigned_BDM__c assgnBDM = new Assigned_BDM__c(Name = 'test', CampaignX__c = proj.Id, Active__c = true);//Add all the required fields
insert assgnBDM;
Project__c proj1 = [SELECT Id, Number_Of_Active_BDMs__c FROM Project__c WHERE Id = :proj.Id];
System.assertEquals(proj1.Number_Of_Active_BDMs__c, 1);
delete assgnBDM;
Project__c proj1 = [SELECT Id, Number_Of_Active_BDMs__c FROM Project__c WHERE Id = :proj.Id];
System.assertEquals(proj1.Number_Of_Active_BDMs__c, 0);
}
}
Regards,
Bhanu Mahesh
All Answers
Try this test class
@isTest
public class ActiveAssignedBDMsTriggerTest{
static testmethod void testInsert(){
Project__c proj = new Project__c(Name = 'Test'); //Add all the required fields
insert proj;
Assigned_BDM__c assgnBDM = new Assigned_BDM__c(Name = 'test', CampaignX__c = proj.Id, Active__c = true);//Add all the required fields
insert assgnBDM;
Project__c proj1 = [SELECT Id, Number_Of_Active_BDMs__c FROM Project__c WHERE Id = :proj.Id];
System.assertEquals(proj1.Number_Of_Active_BDMs__c, 1);
delete assgnBDM;
Project__c proj1 = [SELECT Id, Number_Of_Active_BDMs__c FROM Project__c WHERE Id = :proj.Id];
System.assertEquals(proj1.Number_Of_Active_BDMs__c, 0);
}
}
Regards,
Bhanu Mahesh
Any Ideas?
Oh and one more thing, in my production org there are about 4 ID fields that are required to create both a project and an Assigned BDM, they both lookup to multiple other Objects. How am I to handle that?
Might be Name field on Assigned_BDM__c is an Auto Number. So we cannot populate it. So remove Name from
Assigned_BDM__c assgnBDM = new Assigned_BDM__c( CampaignX__c = proj.Id, Active__c = true);//Add all the required fields
To populate the required lookup fields, first you ned to insert those records and populate those Ids in the respective lookup fields.
Otherwise you can use @isTest(seeAllData = true)
Query all the required data in your test class and try to insert Assigned_BDM__c. But if the records are not alredy present in the org, it will fail.
It is better to insert all the records in the test class. So that it won't fail in any of the sandboxes or production as data is available in test class.
Regards,
Bhanu Mahesh