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
gopal neelurugopal neeluru 

How to write test class for below apex Trigger

trigger Example123 on Case (before insert, before update) {   

  Set<Id> accountIdSet = new Set<Id>();

  for(Case caseForVarRecord : Trigger.new){

    if(caseForVarRecord.AccountId != null ){
      accountIdSet .add(caseForVarRecord.AccountId);
    }        
  }
  
  map<Id,Account> accountMap =  new Map<Id,Account>([Select Id, Ownerid from Account where Id =: accountIdSet ]);

  for (Case c: Trigger.new){ 
    if(c.AdditionalCon__c ==null ){
      c.AdditionalCon__c = accountMap.get(c.AccountId).OwnerId;               
    }
  } 
}
king kullaiking kullai
hai gopal,
             try this test code
@istest private class case_test{
    static testmethod void methodenames(){
        case cse=new case();
            cse.Origin='test';
            cse.Status='testes';
            cse.AdditionalCon__c ='test';
            insert cse;
            
            cse.Origin='test';
            cse.Status='testes';
             cse.AdditionalCon__c ='test';
            update cse;
            Account acc=new Account();
                acc.name='test';
               insert acc;
               acc.name='test';
               update acc;
    }
}
Suneel#8Suneel#8
Below is the test class for your trigger. Code before Test.startTest() line is for the preparation of data for testing the trigger and code after Test.stopTest() line is for verifying the data.Governor limits apply for only lines between Test.startTest() and Test.stopTest().After you insert the case,you need to requery your case and get the data in AdditionalCon__c field to verify the result. For more detailed info check the link -https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods.Replace YOUR_USER_ALIAS with your user alias

 
@isTest
public class TestTriggerExmaple123 {
    public testmethod static void example(){
        Id idey=[select id from user where alias='YOUR_USER_ALIAS'].id;
        Account a=new Account();
        a.name='Test Account';
        a.OwnerId=idey; 
        insert a;
        Case c=new Case();        
        List<Account> l=[select id,ownerid from Account where name='Test Account'];        
        c.accountid=a.id;
        Test.startTest();
        insert c;
        Test.stopTest();
        List<Case> cList=[select AdditionalCon__c  from Case where AccountId=:l[0].id];
        System.assertEquals(idey, cList[0].AdditionalCon__c );
    }
}