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
nununinununi 

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;
    
    }

Best Answer chosen by nununi
Suresh RaghuramSuresh Raghuram

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

Suresh RaghuramSuresh Raghuram

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

This was selected as the best answer
nununinununi

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';

 

nununinununi

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

 

Apex Test Result Detail 
Time Started2/6/2013 7:40 PM
ClassTestUpdateParentaccountName
Method Namemytest
Pass/FailFail
Error MessageSystem.NullPointerException: Attempt to de-reference a null object
Stack TraceClass.TestUpdateParentaccountName.mytest: line 11, column 1
Suresh RaghuramSuresh Raghuram

null pointer exception means your record not inserted.