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
srikanth cheera 6srikanth cheera 6 

write a test class for the map?

public class Task14 {
 
    public static void beforeupdate(Map<id,Account> oldmap,Map<id,Account> newmap){
        list<id> accid=new list<id>();
        for(id key:oldmap.keySet()){
            Account old=oldmap.get(key);
            Account newmp=newmap.get(key);
            if(old.Phone!=newmp.Phone){
              accid.add(key);
            }
        }
        list<Contact> con=[select lastname,firstname,phone,accountid from Contact where accountid in:accid];
        for(Contact c:con){
            Account a=newmap.get(c.accountid);
            Account b=oldmap.get(c.accountid);
            c.Phone=a.Phone;
            c.OtherPhone=b.phone;
            
        }
        update con;
    }
}
==========================================================

trigger Task14Trigger on Account (before update) {
    if(trigger.isbefore && trigger.isupdate){
        Task14.beforeupdate(Trigger.oldmap, Trigger.newmap);
    }
}
====================================================================
Best Answer chosen by srikanth cheera 6
v varaprasadv varaprasad
Hi Srikant,

Please check once sample code below :
 
@isTest
private class Task14_Test {
    
    @isTest static void beforeupdateTest() {
        Map<id,Account> oldmap = new Map<id,Account>();
        Map<id,Account> newmap = new Map<id,Account>();
        Account acct = new Account(Name='Test Account',phone='123456789');
        insert acct;
        oldmap.put(acct.id, acct);
        
        contact con = new contact();
        con.accountid = acct.id;
        con.LastName = 'TestContact';
        insert con;
        
        
        // Perform test
        Test.startTest();
            acct.Phone = '987654321';
            update acct;        
            newmap.put(acct.id, acct);
            
            Task14.beforeupdate(oldmap,newmap);
        Test.stopTest();        
        
        system.assertEquals('987654321', acct.Phone);
    }
    
}

Hope this helps.

Please check once and let me know in case of any help.


Thanks
Varaprasad

All Answers

v varaprasadv varaprasad
Hi Srikant,

Please check once sample code below :
 
@isTest
private class Task14_Test {
    
    @isTest static void beforeupdateTest() {
        Map<id,Account> oldmap = new Map<id,Account>();
        Map<id,Account> newmap = new Map<id,Account>();
        Account acct = new Account(Name='Test Account',phone='123456789');
        insert acct;
        oldmap.put(acct.id, acct);
        
        contact con = new contact();
        con.accountid = acct.id;
        con.LastName = 'TestContact';
        insert con;
        
        
        // Perform test
        Test.startTest();
            acct.Phone = '987654321';
            update acct;        
            newmap.put(acct.id, acct);
            
            Task14.beforeupdate(oldmap,newmap);
        Test.stopTest();        
        
        system.assertEquals('987654321', acct.Phone);
    }
    
}

Hope this helps.

Please check once and let me know in case of any help.


Thanks
Varaprasad
This was selected as the best answer
Suraj TripathiSuraj Tripathi
Hi Srikant ,

Please try this piece of code . 
@istest
public class TestClassTask14 {
    @istest public static void mytest()
    {    
        Account acnt =new Account();
        
 		acnt.Phone='123';
        acnt.name='ajay';       
        insert acnt;
       
        Contact con=new Contact();
        con.firstname = 'ajay';        
        con.Phone=acnt.Phone;
  		con.lastname='testing';
        con.OtherPhone='987';
        con.AccountId=acnt.id;
        insert con;   
        
        acnt.phone = '2002002002';
        update acnt; 
    }
}

Hope it's help you.

Regards
Suraj
srikanth cheera 6srikanth cheera 6
Hii tqs Varaprasad its working
v varaprasadv varaprasad
HI Srikanth,

Kindly mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.


Thanks
Varaprasad
karan singhalkaran singhal
This is not a right approach. As soon as you insert data in your test data, trigger should be fired automatically and coverage should reflect. Hence, instead of step by step covering code approach for trigger, we should follow correct data insertion approach to cover such triggers.
Mohd AshrafMohd Ashraf
Write A Test Class

 public static void updateOpportunityCity(List<Account> newList, Map<Id,Account> oldMap)
    {
        Map<Id,Account> accIdToMap=new Map<Id,Account>();
        List<Opportunity> OppList=new List<Opportunity>();
        for(Account acc: newList)
        {
            if(oldMap!=null && acc.City__c!=oldMap.get(acc.Id).City__c)
            {
                accIdToMap.put(acc.Id,acc);
            }
        }
        for(Opportunity opp :[Select Id,AccountId,City__c From Opportunity Where AccountId IN :accIdToMap.keySet()])
        {
            
            if(accIdToMap.containsKey(opp.AccountId))
            {
               
                opp.City__c=accIdToMap.get(opp.AccountId).City__c;
                oppList.add(opp);
            }
        }
        if(!oppList.isEmpty())
        {
            update oppList;
        }
    }