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
Siva AdminSiva Admin 

Test Class No coverage Help

HI am a newbee to SF. Kindly suggest what's wrong with the below test class. 

@IsTest
public class testclassownerassignmanager{
 static testmethod void metest(){
    Profile pro = [SELECT Id FROM Profile WHERE Name='Standard User']; 
User testUserA = new User(
    Alias = 'standard', Email='standarduser@testorg.com',  
    EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
    LocaleSidKey='en_US', ProfileId = pro.Id, 
    TimeZoneSidKey='America/Los_Angeles', 

UserName='testUserA@testorganise.com'); 
  insert testUserA ;

User testUserB = new User(
    Alias = 'standard', Email='standarduser@testorg.com',  
    EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
    LocaleSidKey='en_US', ProfileId = pro.Id,  
    TimeZoneSidKey='America/Los_Angeles', 
     UserName='testUserB@testorganise.com'); 
     testUserB.ManagerID = testUserA.id;
     
     insert testUserB;


  
  List<Account> acclist= new List<Account>();
  for(Integer i=0;i<=10;i++){
  account a = new account(Name='Test' + i, ownerid=testUserB.id);
    acclist.add(a);
  
  List<Account> TobUpdateAcclist= new List<Account>();
  for(Account ac: acclist){
     if (ac.owner.isactive == false )
      {
       ac.ownerid=testUserB.managerid;
       TobUpdateAcclist.add(a);
      }
      }
  
  test.starttest();
  if (Test.isRunningTest()) {
      System.runAs(new User(Id = Userinfo.getUserId())) {
       update TobUpdateAcclist;
      }
    } else {
       update TobUpdateAcclist;
    }
  test.stoptest();
  
  List<Account> updatedaccs = [select id,name from Account where ID IN: TobUpdateAcclist];
   system.assertEquals(a.owner.Managerid, a.ownerid);
  
  
 }}}
Best Answer chosen by Siva Admin
BALAJI CHBALAJI CH
Hi Siva,
Please find below Test class for your Trigger and Handler class.
 
@IsTest
public class testclassownerassignmanager{
    static testmethod void metest()
    {
        Profile pro = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        
        //Creating testUserA who acts as Manager for another User
        User testUserA = new User(
            Alias = 'standard', Email='standarduser@testorg.com',  
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = pro.Id, 
            TimeZoneSidKey='America/Los_Angeles',
            UserName='testUserA@testorganise.com'); 
        insert testUserA ;

		//Creating testUserB whose manager is testUserA - This user creates few accounts and becomes Inactive
        User testUserB = new User(
            Alias = 'standard', Email='standarduser@testorg.com',  
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = pro.Id,  
            TimeZoneSidKey='America/Los_Angeles', 
            UserName='testUserB@testorganise.com'); 
        testUserB.ManagerID = testUserA.id;
        insert testUserB;
        
        //Insering few Accounts whose Owner is testUserB
        List<Account> acclist= new List<Account>();
        System.runAs (testUserA){
            
            for(Integer i=0;i<=10;i++)
            {
                account a = new account(Name='Test' + i, ownerid=testUserB.id);
                acclist.add(a);
            }
            Insert acclist;
        }
        
        //Inactivate testUserB so that the accounts owner is changed to testUserB's manager i.e, testUserA
        test.starttest();
        testUserB.IsActive = false;
        update testUserB;
        test.stoptest();
          
        Account a = [select id, name, OwnerId from Account where ID IN: acclist limit 1];
        system.assertEquals(a.ownerid, testUserB.Managerid);
    }
}

Let us know if that helps you.


Best Regards,
BALAJI

All Answers

Force Techie 1Force Techie 1
Hi Siva,

What issue you have been faced in above code?

Thanks!
Force Techie
Esther OnemaEsther Onema
Hello,

What is the error message you receive when trying to test this class? Also, if there is a trigger that goes along with it, may you post that as well? 
Thank you,
Esther Onema 
Siva AdminSiva Admin
Not getting coverage
Siva AdminSiva Admin
in trigger, I have collected all the account records whose owner has been deactived and passed those records to this class..

public class AccountHandler {
  @future
  public static void updateAccounts(List<ID> recordIds) {
   List<Account> accts = [SELECT ownerid,owner.ManagerId FROM Account WHERE Id IN :recordIds];
   List<Account> accts1 =new List<Account>();
    for (Account acc:accts ){
    acc.ownerid = acc.owner.ManagerId;
    accts1.add(acc);
   }
    update accts1 ;
    }
}
BALAJI CHBALAJI CH

Hi Siva,

Kindly post your trigger to get a better help.

Best Regrads,
BALAJI

Siva AdminSiva Admin
Hi all Thanks for responding. This is the my trigger for the above class. 

trigger RecordOwnerChangeEx on User (after update) { 
    list<account> lstAcc=new list<account>();
    List<ID> recordIds=new List<ID> ();
    for(Account acc : [select id, ownerid, owner.isActive, owner.ManagerId from account where ownerId IN: trigger.new]){
        if(acc.owner.isActive == false && acc.owner.ManagerId != null){
            //acc.ownerid = acc.owner.ManagerId;
            lstAcc.add(acc);
            recordIds.add(acc.id);
        }
    }
  if(!lstAcc.isEmpty()){
   AccountHandler.updateAccounts(recordIds);
   
   }
}
BALAJI CHBALAJI CH
Hi Siva,
Please find below Test class for your Trigger and Handler class.
 
@IsTest
public class testclassownerassignmanager{
    static testmethod void metest()
    {
        Profile pro = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        
        //Creating testUserA who acts as Manager for another User
        User testUserA = new User(
            Alias = 'standard', Email='standarduser@testorg.com',  
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = pro.Id, 
            TimeZoneSidKey='America/Los_Angeles',
            UserName='testUserA@testorganise.com'); 
        insert testUserA ;

		//Creating testUserB whose manager is testUserA - This user creates few accounts and becomes Inactive
        User testUserB = new User(
            Alias = 'standard', Email='standarduser@testorg.com',  
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = pro.Id,  
            TimeZoneSidKey='America/Los_Angeles', 
            UserName='testUserB@testorganise.com'); 
        testUserB.ManagerID = testUserA.id;
        insert testUserB;
        
        //Insering few Accounts whose Owner is testUserB
        List<Account> acclist= new List<Account>();
        System.runAs (testUserA){
            
            for(Integer i=0;i<=10;i++)
            {
                account a = new account(Name='Test' + i, ownerid=testUserB.id);
                acclist.add(a);
            }
            Insert acclist;
        }
        
        //Inactivate testUserB so that the accounts owner is changed to testUserB's manager i.e, testUserA
        test.starttest();
        testUserB.IsActive = false;
        update testUserB;
        test.stoptest();
          
        Account a = [select id, name, OwnerId from Account where ID IN: acclist limit 1];
        system.assertEquals(a.ownerid, testUserB.Managerid);
    }
}

Let us know if that helps you.


Best Regards,
BALAJI
This was selected as the best answer
Siva AdminSiva Admin
Hi Balaji. thanks for responding This is the error I'm getting, saying "Duplicate value found:duplicates value on record with id:"
User-added image
BALAJI CHBALAJI CH
I saved in my org and able to run successfully. Not sure why you are getting that error.
Can you please try to change the test clas name from 'testclassownerassignmanager' to some other like 'classownerassignmanager_test'.
Siva AdminSiva Admin
HI Balaji....Thanks a Ton..kudos!!!!!!!!!!!!!!!!! Much Appreciated. It did work. 
BALAJI CHBALAJI CH
Its my Pleasure. Happy to see it worked. :-)
Siva AdminSiva Admin
May I know why you've used testuserA in System.Runsas method?
BALAJI CHBALAJI CH
In test class, we are inserting Users and Accounts which throws an Exception: MIXED_DML_OPERATION since User is a Setup Object and Account is a Non-Setup Object. So we are inserting Accounts with another User.