You need to sign in to do that
Don't have an account?
Need help asserting owner in test class
So I've got a trigger that changes the contact owner to the account owner anytime a new contact is created. I am trying to test it but am getting only 80%. I can't figure out how to get it to tell that the change actually happened. I tried adding a system assert message but can't figure out the verbage that it is looking for. If you could take a look and help me out that would be great. The two lines that aren't being covered in my trigger are:
mapAccountToOwner.put(a.Id, a.OwnerId);
and
c.OwnerID = mapAccountToOwner.get(c.AccountId);
and here's the test class I have so far:
@IsTest
private class AccountContactOwner
{
private static TestMethod void testTrigger(){
//Step 1 : Data Insertion
Account a=new Account(Name='Test Account');
insert a;
Contact c = new Contact(FirstName='John',LastName='Doe');
insert c;
test.startTest();
//Perform the dml action on which trigger gets fired , like insert, update ,delete , undelete, in your case you have to update account record that you created in above
//create test user
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
User u1 = new User(Alias = 'standa', Email='saplingstandarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='saplingstandarduser@testorg.com');
System.runAs(u1) {
// The following code runs as user 'u'
// create test contact
Contact c1 = new Contact(FirstName='Jane',LastName='Doe');
insert c1;
//assert your results using system.assert and system.asserEquals
system.assert(c.OwnerID = a.OwnerID);
test.stopTest();
}
}
}
Hi,
On your contacts in your test method you will need to set the Account to be your test account Try something like this:-
Good luck!
Rich.
All Answers
Hi,
Can you show your trigger code?
Thanks,
Babu.
I'm guessing that it wants me to assert anyways, based on the 2 lines that aren't being covered. Here's the full trigger.
Hi,
On your contacts in your test method you will need to set the Account to be your test account Try something like this:-
Good luck!
Rich.
It still doesn't like the assert
I'm getting
Method does not exist or incorrect signature: system.assert(Id) at line 38 column 9
Thanks Rich!
Had to change it just a little bit (it wanted Account.id in places and ==) but that was what I needed! Here's the corrected working code if anyone in the future needs it.