You need to sign in to do that
Don't have an account?

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;
}
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;
}
Thanks,
AMit Singh
All Answers
Thanks,
AMit Singh
@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.
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);
}
}
AMit it's working Fine.