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

Test class for update trigger
Hi,
Here i attached my Update trigger and test class method. Test class method fails.Please help me where i went wrong.
////Trigger
trigger updateContactOpportunity on Account (before update) {
Set<Id> ids=new Set<Id>();
for(Account a:Trigger.new){
ids.add(a.id);
}
List<Contact> con=[Select id, accountId, phone from Contact where accountId in:ids];
List<Opportunity> opp= [Select id, AccountId, Custom_Opportunity_Number__c from Opportunity where accountId in:ids];
for(Account a:Trigger.new){
for(integer j=0;j<con.size();j++){
if(a.id==con[j].accountId){
con[j].phone=a.phone;
}
}
for(integer i=0; i<opp.size();i++){
if(a.id==opp[i].accountid){
opp[i].Custom_Opportunity_Number__c=a.Custom_Account_Number__c;
}
}
}
update con;
update opp;
}
////TestClass
@isTest
private class testupdateConOpp{
static testMethod void testUpdate(){
Account a=new Account(name='test',phone='2234',Custom_Account_Number__c='6537');
insert a;
Contact c= new Contact(lastName='test', accountId='test.id',phone='2234');
insert c;
Opportunity o=new Opportunity(accountId='a.id', Custom_Opportunity_Number__c='6537');
insert o;
a.phone='111';
a.Custom_Account_Number__c='222';
update a;
Contact updatedContact=[Select accountid, phone from Contact where id =:a.id ];
system.assertEquals(updatedContact.phone,'111');
Opportunity updatedOpp=[Select accountId,Custom_Opportunity_Number__c from opportunity where id=:a.id];
system.assertEquals(updatedOpp.Custom_Opportunity_Number__c,'222');
}
}
This is your test class
All Answers
Test Cass
===================================================
@isTest
private class testupdateConOpp{
static testMethod void testUpdate(){
Account a=new Account(name='test',phone='2234',Custom_Account_Number__c='6537');
insert a;
Contact c= new Contact(lastName='test', accountId=a.Id,phone='2234');
insert c;
Opportunity o=new Opportunity(accountId=a.id, Custom_Opportunity_Number__c='6537');
insert o;
a.phone='111';
a.Custom_Account_Number__c='222';
update a;
Contact updatedContact=[Select accountid, phone from Contact where id =:a.id ];
system.assertEquals(updatedContact.phone,'111');
Opportunity updatedOpp=[Select accountId,Custom_Opportunity_Number__c from opportunity where id=:a.id];
system.assertEquals(updatedOpp.Custom_Opportunity_Number__c,'222');
}
}
what error or y test class is failing?
Class.testupdateConOpp.testUpdate: line 7,
when it's a contact record
new Contact(AccountId = acc.Id);
for Opportunity
new Opportunity(AccountId = account.Id);
This is your test class
@isTest
private class testupdateConOpp{
static testMethod void testUpdate(){
Account a=new Account(name='test',phone='2234',Custom_Account_Number__c='6537');
Hi,
Thanks to all. I found my mistakes.
//My code:
Contact c= new Contact(lastName='test', accountId='a.Id', phone='2234');
insert c;
Opportunity o=new Opportunity( accountId='a.id', Custom_Opportunity_Number__c ='6537');
insert o;
//Correction:
1.AccountId=a.id
2.In opportunity I inserted accountId and Custom_Opportunity_Number__c only.
Not Required fields
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name, StageName, CloseDate]: [Name, StageName, CloseDate]
Really thanks to all.