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

Test Class For Custom Object

Hi everyone-


I've developed a trigger that works as intended, but I'm having trouble developing a test class that gets coverage.  Here is the trigger code:


trigger ReportsToDefensive on Defensive_Tracking__c (before insert, before update) {
	//Pull reports_to__c from user record
       // create a set of all the unique ownerIds

  Set<Id> ownerIds = new Set<Id>(); for (Defensive_Tracking__c d : ownerIds.add(d.Account_Owner__c); // query for all the User records for the unique userIds in the records // create a map for a lookup / hash table for the user info
 Map<Id, User> owners = new Map<Id, User>([Select USER.Reports_To__c from User Where Id
in:ownerIds]); // iterate over the list of records being processed in the trigger and // set the field values before insert & update

 for (Defensive_Tracking__c d: { d.Reports_To__c = owners.get(d.Account_Owner__c).Reports_To__c; } }

And here is the corresponding test class I wrote that I cannot get coverage with:


private class ReportsToDefensiveTest {

  static testMethod void testReportsToDefensive() {
        // TO DO: implement unit test

   User u = [Select id from User where id =: Userinfo.getuserId()];
   u.Reports_To__c = '00500000006xlrN';
   update u;

   system.runAs(u) {
   Defensive_Tracking__c d = new Defensive_Tracking__c(Account__c =    '001R000000dlCW1', 	Account_Owner__c = '005000000074MeF', 
   		of_Units_under_Raid_Risk__c = 12, Reason_why_at_Risk__c = 'Test', CT_Unit_Price_at_Time_of_Raid_Risk__c = 4,
   			Total_Annual_Rep_Rev_at_Time_of_Risk__c = 1, Competitor__c = 'Test' );
   //fill all mandatory field of account before insert if any
   insert d;
   d = [select id, Reports_to__c from Defensive_Tracking__c where id];
   //requery account a after insert
   system.assertEquals(u.Reports_To__c, d.Reports_To__c);


 The various fields that I included are required based off of validation rules.  Can somebody please help me modify the test class so I get full coverage?


Anybody have any ideas?  I would greatly appreciate it.