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

Help on Writting Apex Test Class
Hello! This is my trigger. If the Account filed on Contact is not equal to "Not Available" then it populates Parent Account Field on Contacts with the Account.Parent.
Trigger updateParentAccountName on Contact(before insert,before update){ List<Contact> conList = new List<Contact>(); List<Contact> accList = new List<Contact>(); List<Id> idList = new List<Id>(); for(Contact con :Trigger.new){ if(con.Account.Name != 'Not Available'){ idList.add(con.AccountId); } } Map<Id,Account>accMap = new Map<Id,Account>([select ParentId from Account where id in:idList]); for(Contact c : trigger.new){ if(c.Account.Name != 'Not Available'){ c.Parent_Name__c = accMap.get(c.AccountId).ParentId; } } } |
Can anyone help me write a test class for this?
I have written the following but doesnt seem to work.
@isTest
private class TestUpdateParentaccountName
{
static testMethod void mytest()
{
//Create the 2 different categories the contact name may fall in
List<Contact> testContacts = new List<Contact>();
Contact testCase = new Contact();
testCase.LastName = 'test1';
testCase.Account.Name = 'Food and Drug Administration';
testContacts.add(testCase);
Contact testCase2 = new Contact();
testCase2.LastName = 'test2';
testCase2.Account.Name = 'Not Available';
testContacts.add(testCase2);
insert testContacts;
}
}
I would like to suggest you one thing before guiding you to write test class.
For collecting ids u used List<Id>, better use set<Id>. go through the difference b/w the set and list, then you will decide which is best.
for the test class
@isTest
private class TestUpdateParentaccountName
{
static testMethod void mytest()
{
//Create the 2 different categories the contact name may fall in
List<Contact> testContacts = new List<Contact>();
Contact testCase = new Contact();
testCase.LastName = 'test1';
testCase.Account.Name = 'Food and Drug Administration';
Test.startTest();
insert testcase;
Update testCase.Parent_Name__c = 'xyz';
Test.stopTest();
Contact ct=[select Parent_Name__c from Contact where id =: testCase.Id];
system.assert(Parent_Name__c =='xyz', ' PArent not changed');
}
}
in the above code i tried to give you just an idea .
follow the link and learn more abt test classes.
If this helps you make this as solution and mark the kudos.
http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_methods_system_test.htm|StartTopic=Content%2Fapex_methods_system_test.htm|SkinName=webhelp
All Answers
I would like to suggest you one thing before guiding you to write test class.
For collecting ids u used List<Id>, better use set<Id>. go through the difference b/w the set and list, then you will decide which is best.
for the test class
@isTest
private class TestUpdateParentaccountName
{
static testMethod void mytest()
{
//Create the 2 different categories the contact name may fall in
List<Contact> testContacts = new List<Contact>();
Contact testCase = new Contact();
testCase.LastName = 'test1';
testCase.Account.Name = 'Food and Drug Administration';
Test.startTest();
insert testcase;
Update testCase.Parent_Name__c = 'xyz';
Test.stopTest();
Contact ct=[select Parent_Name__c from Contact where id =: testCase.Id];
system.assert(Parent_Name__c =='xyz', ' PArent not changed');
}
}
in the above code i tried to give you just an idea .
follow the link and learn more abt test classes.
If this helps you make this as solution and mark the kudos.
http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_methods_system_test.htm|StartTopic=Content%2Fapex_methods_system_test.htm|SkinName=webhelp
Thanks a lot for your reply.
I copy pasted the test class that you gave me. It threw an error on this line:
system.assert(Parent_Name__c =='xyz', ' PArent not changed');
So i replaced it with:
system.assert(ct.Parent_Name__c =='xyz', ' PArent not changed');
Its now throwing another error: Error: Compile Error: DML requires SObject or SObject list type: Id at line 14 column 5
Line 14 is Update testCase.Parent_Name__c = 'xyz';
Hi Suree,
another update: I changed this:
update testCase.Parent_Name__c = 'Department of Health and Human Services';
to
testCase.Parent_Name__c = 'Department of Health and Human Services';
update testCase;
That took care of the error, but the test class fails
null pointer exception means your record not inserted.