+ Start a Discussion
SFDCDevQASFDCDevQA 

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

Best Answer chosen by Admin (Salesforce Developers) 
Richie DRichie D

Hi,

 

On your contacts in your test method you will need to set the Account to be your test account Try something like this:-

@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(Account=a, 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', Account=a);
        insert c1;
        
        //assert your results using system.assert and system.asserEquals

//reload objects to make sure values are loaded
        c1 = [select id, name, Account, OwnerId from contact where id=:c.id];
c = [select id, name, Account, OwnerId from contact where id=:c.id];

a= [select id, ownerId from Account where id=:a.id];
        system.assert(c1.OwnerID = a.OwnerID);
        
        
        
        test.stopTest();
    }
}
}

 Good luck!

Rich.

All Answers

JBabuJBabu

Hi,

 

Can you show your trigger code?

 

Thanks,

Babu.

SFDCDevQASFDCDevQA

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.

 

trigger ContactOwnerUpdate on Contact (before insert,before update) {
// gather all the Account Id's in a set

   Set<Id> setAccountIds = new Set<Id>();

   Contact[] con = Trigger.new;
   for (Contact c:con)

   {
      setAccountIds.add(c.AccountId);

   }

  

   // store each Account Id with it's owner Id in a map, such that AccountId => Account.OwnerId

   Map<Id, Id> mapAccountToOwner = new Map<Id, Id>();

   for(Account a : [Select Id, OwnerId From Account Where Id IN : setAccountIds])

   {

      mapAccountToOwner.put(a.Id, a.OwnerId);
   }
   for (Contact c:con)
    {
       if(c.AccountId !=null)
       {
           c.OwnerID = mapAccountToOwner.get(c.AccountId); // here you are avoiding the query
       }
   }  
}
Richie DRichie D

Hi,

 

On your contacts in your test method you will need to set the Account to be your test account Try something like this:-

@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(Account=a, 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', Account=a);
        insert c1;
        
        //assert your results using system.assert and system.asserEquals

//reload objects to make sure values are loaded
        c1 = [select id, name, Account, OwnerId from contact where id=:c.id];
c = [select id, name, Account, OwnerId from contact where id=:c.id];

a= [select id, ownerId from Account where id=:a.id];
        system.assert(c1.OwnerID = a.OwnerID);
        
        
        
        test.stopTest();
    }
}
}

 Good luck!

Rich.

This was selected as the best answer
SFDCDevQASFDCDevQA

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

SFDCDevQASFDCDevQA

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.

 

@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(Accountid=a.id, 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', Accountid=a.id);
        insert c1;
        
        //assert your results using system.assert and system.asserEquals

//reload objects to make sure values are loaded
        c1 = [select id, name, Account.id, OwnerId from contact where id=:c1.id];
c = [select id, name, Account.id, OwnerId from contact where id=:c.id];

a= [select id, ownerId from Account where id=:a.id];
        system.assert(c1.OwnerID == a.OwnerID);
        
        
        
        test.stopTest();
    }
}
}