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

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){
this is what i came up with so far:
@isTest
I get 100% code coverage but the test method fails. This is the error that i get
Any help would be appreciated! Thanks!! |
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
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.
Hi,
Please make the following change in your test class from
@isTest
to
@isTest(SeeAllData=true)
I changed to SeeAllData=true. But still doesn't pass.
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');
}
}
tried it. Got this
Class.TestContactTriggers.mytest: line 21, column 1
I do have an account called " Department of Health and Human Services" set up.
Actually it worked. Had my account names mixed up. Thanks!!