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
Amit Khanduri.Amit Khanduri. 

Please correct my test class where am wrong .. as am new.

public without sharing class OpportunityUtility {
    public static void assignAccountToAM(String opportunityId){
        Opportunity oppRec = [Select Id, AccountId,OwnerId,Owner.Account_Manager__r.Id,
            Owner.Account_Manager__c,Account.OwnerId,Account.Owner.Is_Account_Manager__c from Opportunity where Id =: opportunityId];
        if(oppRec.Account.Owner.Is_Account_Manager__c == false && 
                oppRec.Owner.Account_Manager__c != null && 
                        oppRec.Owner.Account_Manager__r.Id != oppRec.Account.OwnerId){
            Account accountRec = [Select Id,OwnerId from Account where Id=: oppRec.AccountId];
            accountRec.OwnerId =oppRec.Owner.Account_Manager__c;
            update accountRec; 
        }
    }
}

@isTest(seeallData=false)
public  class TestClassforOpportunityUtilityclass {
    
     static void firstOpp(){
        User u1= [select id from User where alias='AUser' AND Is_Account_Manager__c = false limit 1];
      update u1;
        
        User u2= [select id from User where alias='AaUser' AND Is_Account_Manager__c = true limit 1];
      update u2;
        
    
date dt= date.today();  
       dt = dt-90;
       Date dt1 = dt.toStartOfMonth();
       integer noOfDays = date.daysInMonth(dt1.year(),dt1.month());
       Date dt2 = date.newInstance(dt1.year(),dt1.month(),noOfDays);
List<Opportunity> optys= new List<Opportunity>();

Opportunity opty = new Opportunity();
opty.StageName = 'warm';
opty.CloseDate= dt;
opty.CreatedDate=dt2;
opty.Accountid ='aks';
opty.Account.Owner.Is_Account_Manager__c = false;
opty.Account.owner.id = u1.id;
opty.Owner.Account_Manager__r.Id = u2.id;
opty.name='optytest';
opty.Opplead_id__c='testid';
opty.Division__c='Jaguars';
         Optys.add(opty);
 insert optys;
         
         for(Opportunity loopopp : optys) {
           System.assertEquals(loopopp.Account.owner.id, loopopp.Owner.Account_Manager__c);
             System.debug('loopopp');
           // loopopp.Account.owner.id = optys.get(Owner.id).Account_Manager__c;
         
         update loopopp;
     }
    }
     
}

Last three line of my class is showing not tested please rectify my test class.
Alba RivasAlba Rivas
Hi Amit

I will give you some tips. The first thing is I would recommend you to use directly an Id as parameter in your OpportunityUtility class rather than a String. In Salesforce Ids can have 15 or 18 characters, then using a String as Id could give wrong results, for example in comparations.

Regarding your test class, in the first lines you don't have to update the user records (u1, u2), as you are not updating any field on them! Retrieving their Id is enough for your purpose.

In the test I am not seeing any call to assignAccountToAM method, I suposse you are calling that class in the opportunity trigger?

Finally, the update inside the for loop is not needed either.

Best regards