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
InternalServerErrorInternalServerError 

Testing Bulk Update (system.assertequals)

Hey guys, I have created a test class, I am  new to apex code so I have been taking a bit from here and there. Basically the first part of the class works but the bulk validation is not working I want to use system.assertequals to verify that the value on the update field is equal to the expected value, how do I do this with a list of records

 

This is the test class, the part that is not working is commented at the end. The error I am getting when running the test is System.AssertException: Assertion Failed: Expected: 005d0000000jTxVAAU, Actual: null  I think I know the reason for the error I just dont know how to bulk validate the value returned as I did with the single validation.

 

Many thanks in advance.

 

@isTest(SeeAllData=true)

private class TestClassCaseLookupTrigger {

    /* Tests a user creating a case and then updating the owner to a queue,
    then updates the ownership from a queue to another user,
    this should cause the lookup field New_Owner__c be updated with the same id as the new owner*/

    //Tests single and mass updates
    
    static testMethod void singleandmassupdates() {
                  
              
       
        
        List<Queuesobject> queues=[SELECT Queue.Name, QueueId FROM QueueSobject 
                   WHERE SobjectType = 'Case'];
        
        Map<String,String> CaseQueues = new Map<String,String>{};
        for(QueueSobject q:queues)
            Casequeues.put(q.Queue.Name,q.QueueId);
            
            
        List<User> userlist=[SELECT Alias, Id FROM User 
                    WHERE UserType != 'CSNOnly' AND IsActive = TRUE];
        Map<String,String> usermap= new map<string,string>{};
        for(User us:userlist)
            usermap.put(us.Alias,us.Id);
            
        //Set up user 
            User testuser = [SELECT Id FROM User WHERE Alias='cbrow'];
            
        //Run as test user
        System.RunAs(testuser){

            
        System.debug('Inserting a case..');
        
        //creating and updating a single case
        
        Case testCase = new Case(status = 'New', Origin='Phone');
        insert testCase;
        System.debug('Changing ownership to queue..');
        testCase.ownerid=CaseQueues.get('L1 EMEA Queue');
        update testCase;
        System.debug('Changing ownership to another user..');
        testCase.ownerid=usermap.get('BChir');
        update testCase;
        
        Case result = [select New_Owner__c from Case where Id = :testCase.Id];
        System.assertEquals(usermap.get('BChir'),result.New_Owner__c);
        
        
       System.debug('Inserting and updating 200 cases...bulk validation');
             
       List<Case> testCases = new List<Case>();
        for(integer i=0; i<200; i++) {
            testCases.add( new Case(status ='New',Origin='Phone',Ownerid=usermap.get('BChir')) );
        }
        insert testCases;
        
        //change the ownership from a queue to a user on list of cases created before 
        
        for(integer i=0; i<200; i++) {

        testCases[i].OwnerId=CaseQueues.get('L1 EMEA Queue');
        }
        
        update testCases;
        
        
        for(integer i=0; i<200; i++){
        
        System.assertEquals(usermap.get('BChir'),TestCases[i].New_Owner__c);  //this is not working
        }
     }
    
   }
  
  }
Rajesh SriramuluRajesh Sriramulu

Hi

 

@isTest(SeeAllData=true)

private class TestClassCaseLookupTrigger {

    /* Tests a user creating a case and then updating the owner to a queue,
    then updates the ownership from a queue to another user,
    this should cause the lookup field New_Owner__c be updated with the same id as the new owner*/

    //Tests single and mass updates
    
    static testMethod void singleandmassupdates() {
                  
              
       
        
        List<Queuesobject> queues=[SELECT Queue.Name, QueueId FROM QueueSobject 
                   WHERE SobjectType = 'Case'];
        
        Map<String,String> CaseQueues = new Map<String,String>{};
        for(QueueSobject q:queues)
            Casequeues.put(q.Queue.Name,q.QueueId);
            
            
        List<User> userlist=[SELECT Alias, Id FROM User 
                    WHERE UserType != 'CSNOnly' AND IsActive = TRUE];
        Map<String,String> usermap= new map<string,string>{};
        for(User us:userlist)
            usermap.put(us.Alias,us.Id);
            
        //Set up user 
            User testuser = [SELECT Id FROM User WHERE Alias='cbrow'];
            
        //Run as test user
        System.RunAs(testuser){

            
        System.debug('Inserting a case..');
        
        //creating and updating a single case
        
        Case testCase = new Case(status = 'New', Origin='Phone');
        insert testCase;
        System.debug('Changing ownership to queue..');
        testCase.ownerid=CaseQueues.get('L1 EMEA Queue');
        update testCase;
        System.debug('Changing ownership to another user..');
        testCase.ownerid=usermap.get('BChir');
        update testCase;
        
        Case result = [select New_Owner__c from Case where Id = :testCase.Id];
system.debug('result555555'+result);
        System.assertEquals(usermap.get('BChir'),result.New_Owner__c);
        
        
       System.debug('Inserting and updating 200 cases...bulk validation');
             
       List<Case> testCases = new List<Case>();
        for(integer i=0; i<200; i++) {
            testCases.add( new Case(status ='New',Origin='Phone',Ownerid=usermap.get('BChir')) );
        }
        insert testCases;
        
        //change the ownership from a queue to a user on list of cases created before 
        
        for(integer i=0; i<200; i++) {

        testCases[i].OwnerId=CaseQueues.get('L1 EMEA Queue');
        }
        
        update testCases;
        
        
        for(integer i=0; i<200; i++){
        
        System.assertEquals(usermap.get('BChir'),TestCases[i].New_Owner__c);  //yes here it is getting the null value see query where u fetech to get that value this is not working
        }
     }
    
   }
  
  }

 

Here I put some debug statement and see the debug values and if possible put some more debug statements so that u can get some id that value can be put in system,asser.

 

Regards,

Rajesh.

InternalServerErrorInternalServerError

Thanks Rajesh, but that is the actual problem, I don't know how to query on the ids and values on my New_Owner__c field in the list of 200 records updated in a similar way to what I did in the single validation test, how can I query the list of 200 records to the comparte in the system.assertequals ?

 

Cheers