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
DeveloperDeveloper 

please help me , how to write test class for bellow code.

trigger contactnumberchange on Contact (before insert,before update) { 
    List<account> li=new list<Account>();  
    List<Id> ids = new List<Id>();  
    for(Contact c: trigger.new)  
        ids.add(c.AccountId);  
    Map<Id, Account> accountMap = new Map<Id, Account>([Select Id, Phone From Account Where Id In :ids]); 
 
    for(Contact c: trigger.new) 
    { 
        Account a = accountMap.get(c.AccountId); 
        if(a != null) 
        { 
            a.Phone= c.MobilePhone; 

            li.add(a); 
        } 
    } 
    update li;  
}
Best Answer chosen by Developer
Amit Singh 1Amit Singh 1
Ok try below code 
@isTest
private class contactnumberchange_Test{
  static testMethod void test1(){
    Account acc = new Accunt(Name='Test Account', Phone='9807213456');
    insert acc;
    Contact c = new Contact(LastName='Test COntact', AccountId = acc.id, MobilePhone='1234567890');
    insert c;
    c.FirstName ='Updated';
    c.MobilePhone = '0987654321';
    update c;
}
}
Let me know if this helps :)

Thanks,
AMit Singh
 

All Answers

Amit Singh 1Amit Singh 1
Ok try below code 
@isTest
private class contactnumberchange_Test{
  static testMethod void test1(){
    Account acc = new Accunt(Name='Test Account', Phone='9807213456');
    insert acc;
    Contact c = new Contact(LastName='Test COntact', AccountId = acc.id, MobilePhone='1234567890');
    insert c;
    c.FirstName ='Updated';
    c.MobilePhone = '0987654321';
    update c;
}
}
Let me know if this helps :)

Thanks,
AMit Singh
 
This was selected as the best answer
Mohan ChanderMohan Chander
Hi Gopal,

@isTest
public class ContactNumberChangeTest {

    @isTest
    public static void testContactNumberChange(){
        Account acc = new Account(Name = 'ATS Computing');
        insert acc;
        Contact con = new Contact();
        con.AccountId = acc.Id;
        con.FirstName = 'Gopal';
        con.LastName = 'M';
        con.MobilePhone = '123';
        insert con;
        System.assertEquals(acc.Phone, con.MobilePhone);
        con.MobilePhone = '1234';
        update con;
        System.assertEquals(acc.Phone, con.MobilePhone);
    }
}

This will cover 100%. Please mark the answer as best answer if you find this useful.
Roshni RahulRoshni Rahul
Hi Gopal,
Please try this code. I have done bulk testing along with it.

@isTest
public class ContactNumberChangeTest {

    @isTest
    public static void testContactNumberChange(){
        Account acc = new Account(Name = 'test');
        insert acc;
        Contact con = new Contact();
        con.AccountId = acc.Id;
        con.FirstName = 'TestName';
        con.LastName = 'TestLastName';
        con.MobilePhone = '123';
        insert con;
        System.assertEquals(acc.Phone, con.MobilePhone);
        con.MobilePhone = '1234';
        update con;
        System.assertEquals(acc.Phone, con.MobilePhone);
    }

    public static TestMethod void testBulk(){
        Integer bulkLimit=100;
        Account acc = new Account(Name = 'Test');
        insert acc;
       List<Contact> testcontact =new List<Contact>();
        insert con;
        for(Integer i = 0; i < bulkLimit ; i++){
      Contact con = new Contact();
        con.AccountId = acc.Id;
        con.FirstName = 'TestName'+i;
        con.LastName = 'TestLastName';
        con.MobilePhone = '123';
testcontact.add(con);
        }
        insert testcontact;
        System.assertEquals(acc.Phone, testcontact[0].MobilePhone);


    }

   
}
Leo10Leo10
Hi,
@isTest 
private class ClassName_Test {
    static testMethod void testMethodName() {

        Account testAccount = new Account();
        testAccount.Name='Test Account' ;
        testAccount .Phone='1111111111'
        insert testAccount;
        
        Contact cont = new Contact();
        cont.FirstName='Test';
        cont.LastName='Test';
        cont.Accountid= testAccount.id;
        insert cont;
    }
}

 
DeveloperDeveloper
Thanks,
AMit it's working Fine.