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
Chinnu ChinnuChinnu Chinnu 

can anyone please tell me how to write test class for this trigger class


trigger updateAssessmentValue on Assessment__c(after insert, after update) {
  Map<ID, Building__c> parentBuild = new Map<ID, Building__c>(); 
  List<Id> listIds = new List<Id>();

  for (Assessment__c childObj : Trigger.new){
    listIds.add(childObj.Building__c);
  }

  
  parentBuild = new Map<Id, Building__c>([SELECT id, Recent_Assessment_Date__c ,Recent_Assessment_Value__c ,(SELECT ID, Assessment_Value__c,Assessment_Date__c FROM Assessments__r) FROM Building__c WHERE ID IN :listIds]);

  for (Assessment__c assess : Trigger.new){
     Building__c myBuild = parentBuild.get(assess.Building__c);
     myBuild.Recent_Assessment_Date__c = assess.Assessment_Date__c ;
     myBuild.Recent_Assessment_Value__c = assess.Assessment_Value__c;
  }

  update parentBuild.values();
}
Best Answer chosen by Chinnu Chinnu
Boss CoffeeBoss Coffee
Try using the following to model what your test class will look like. I made assumptions as to what were required fields and what the Assessment lookup field api name would be.
@isTest
public class AssessmentTriggerTest {
    
    // Set up the records to be tested
    @testSetup
    static void createRecords() {
        // Create five buildings
        List<Building__c> buildings = new List<Building__c>();
        for(Integer i = 0; i < 5; i++) {
            // Create building with required fields
            buildings.add(new Building__c(
            	Name='testBuilding' + i
            ));
        }
        insert buildings;
    }
    
    // Test insertion of Assessments
    @isTest
    public static void testAssessmentInsert() {
        test.startTest();
        // Set up an assessment for each Building
        List<Building__c> buildings = [SELECT Id FROM Building__c];
        List<Assessment__c> assessments = new List<Assessment__c>();
        for(Building__c b : buildings) {
            assessments.add(new Assessment__c(
            	// Create your assessments with expected fields and lookup to Building
                Assessment_Date__c = Date.today(),
                Assessment_value__c = 5,
                Building__c = b.Id
            ));
        }
        insert assessments;
        test.stopTest();
        
        // Make sure there are the five existing Building__c records
        List<Building__c> buildings = [
            SELECT Id, Recent_Assessment_Date__c, Recent_Assessment_Value__c, (SELECT Id, Assessment_Value__c, Assessment_Date__c FROM Assessments__r)
            FROM Building__c
        ];
        System.assertEquals(5, buildings.size());
        
        // Make sure that the dates and values were correctly assigned
        for(Building__c building : buildings) {
            System.assertEquals(building.Assessments[0].Assessment_Date__c, building.Recent_Assessment_Date__c);
            System.assertEquals(building.Assessments[0].Assessment_Value__c, building.Recent_Assessment_Value__c);
        }
    }
}