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 

Apex test Class Issues.

I am trying to write an apex test class for the following trigger

 

Trigger updateParentAccountName on Contact(before insert, before update){

List<Id> idList = new List<Id>();

for(Contact con : trigger.new ){
    if(con.AccountId != '001Q000000b32BD'){
        idList.add(con.AccountId);
        Map<Id,Account>accMap = new Map<Id,Account>([select ParentId from Account where id in:idList]);
        con.Parent_Account__c = accMap.get(con.AccountId).ParentId;
        }
    }
}

 

this is what i came up with so far:

 

@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.AccountId = '001Q000000agMR5';
    testCase.RPCD_Functional_Area__c = 'General';
Test.startTest();
 insert testCase;
    testCase.Parent_Account__c = 'Department of Health and Human Services';
    update testCase;
 Test.stopTest();
Contact ct=[select  Parent_Account__c from Contact where id =: testCase.Id];
system.assert(ct.Parent_Account__c =='Department of Health and Human Services', ' Parent not changed');
    }
}

 

I get 100% code coverage but the test method fails. This is the error that i get

 

Time Started2/13/2013 9:45 AM
ClassTestUpdateParentaccountName
Method Namemytest
Pass/FailFail
Error MessageSystem.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateParentAccountName: execution of BeforeInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.updateParentAccountName: line 9, column 1: []
Stack Trace

Class.TestUpdateParentaccountName.mytest: line 14, column 1

 

Any help would be appreciated! Thanks!!

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

Hi,

 

Try the below code :-

 

@isTest(SeeAllData=true)
private class TestUpdateParentaccountName
{
static testMethod void mytest()
{
//Create the 2 different categories the contact name may fall in

Account acc = new Account(name='Test Account');
insert acc;

Account parentAcc = new Account(name='Parent Account',ParentId=acc.id);
insert parentAcc;

Contact testCase = new Contact(lastname='Test',AccountId=parentAcc.id,RPCD_Functional_Area__c = 'General');


Test.startTest();
insert testCase;
Test.stopTest();
Contact ct=[select Parent_Account__c from Contact where id =: testCase.Id];
system.assertequals(ct.Parent_Account__c,'Test Account');
}
}

All Answers

Jeff MayJeff May

A couple of comments

 

1) you should move the idList building into it own loop (and use a Set<Id> instead so the keys are unique.

 

for(Contact con : trigger.new){

   idList.add(.....

}

 

then make a single SELECT using the entire set of Ids

 

Then, in your main trigger loop, you'll want to check to make sure that

 

accMap.containsKey(con.AccountId) 

 

before trying to get the ParentId of the mapped Account.

Vinit_KumarVinit_Kumar

Hi,

 

Please make the following change in your test class from

 

@isTest

 

to 

 

@isTest(SeeAllData=true)

nununinununi

I changed to SeeAllData=true. But still doesn't pass. 

Vinit_KumarVinit_Kumar

Hi,

 

Try the below code :-

 

@isTest(SeeAllData=true)
private class TestUpdateParentaccountName
{
static testMethod void mytest()
{
//Create the 2 different categories the contact name may fall in

Account acc = new Account(name='Test Account');
insert acc;

Account parentAcc = new Account(name='Parent Account',ParentId=acc.id);
insert parentAcc;

Contact testCase = new Contact(lastname='Test',AccountId=parentAcc.id,RPCD_Functional_Area__c = 'General');


Test.startTest();
insert testCase;
Test.stopTest();
Contact ct=[select Parent_Account__c from Contact where id =: testCase.Id];
system.assertequals(ct.Parent_Account__c,'Test Account');
}
}

This was selected as the best answer
nununinununi

tried it. Got this

 

Time Started2/13/2013 11:01 AM
ClassTestContactTriggers
Method Namemytest
Pass/FailFail
Error MessageSystem.StringException: Invalid id: Department of Health and Human Services
Stack Trace

Class.TestContactTriggers.mytest: line 21, column 1

 

I do have an account called " Department of Health and Human Services" set up.

nununinununi

Actually it worked. Had my account names mixed up. Thanks!!