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
chanbasha nbskchanbasha nbsk 

Test class creation

HI,


    How to write the test class for the below code 


    public class CaseUserHelper {

    public static void updateCaseStage(List<Case> cases) {
    
             List<ID> cIds = new List<ID>();
        
             List<Case_User__c> cuser = New  List<Case_User__c>();
             Map<ID, Case_User__c> cusermap = new Map<ID, Case_User__c>();

             Set<String> skillSet = new Set<String>();
             List<String> skillSetList = new List<String>();
    
             for(Case c: cases){
        
                   if(c.Status == 'New') {
                   cIds.add(c.Id); //add case ids.
                   }     
                   skillSet.add(c.CaseRelatedTo__c);
             }   
           skillSetList.addAll(skillSet);    
        
         //available users               
           try{
                   List<Case_User__c> availableCaseUsers = [SELECT AvailableUsers__c, Timings__c, SkillSet__c  FROM Case_User__c where Availability__c = TRUE ORDER BY Last_Case_Allocated_Time__c ASC];
                 //List<Case_User__c> availableCaseUsers = Database.query(queryString);
                    integer i=0;
                    integer nomatchingSkillset = availableCaseUsers.size();
                    for(Case c: cases){
                        for(; i<=availableCaseUsers.size();i++){
                        if(availableCaseUsers.get(i).SkillSet__c.contains(c.CaseRelatedTo__c)){
                          c.OwnerId = availableCaseUsers.get(i).AvailableUsers__c;
                          availableCaseUsers.get(i).Last_Case_Allocated_Time__c = DateTime.now();
                          cusermap.put(availableCaseUsers.get(i).id, availableCaseUsers.get(i));
                         } else {
                              nomatchingSkillset--;
                              if(nomatchingSkillset == 0){
                                  //TODO assign to case queue
                                  
                                     User u =[SELECT Email FROM User WHERE Id IN (SELECT UserOrGroupId FROM GroupMember WHERE Group.DeveloperName ='CaseQueue')];
                                     String us = u.Email;
                                                          
                                      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                                      String[] toAddresses = new String[] {us};
                                      mail.setToAddresses(toAddresses );
                                      mail.setSubject('No Users available to handle this case');
                                      mail.plainTextBody='handle this case';
                                      Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
                                                          
                                  break;
                              }
                              continue;
                        }
                        if(i>=availableCaseUsers.size()){
                            i=0;
                        }
                        break;
                     }      
                }
            }catch(System.DmlException e){
            
                 System.debug('Missing the required field in record');
                 
            }catch(Exception e){
            
                 System.debug('An Error Occured at:' +e.getMessage());
                 
            }finally{
            
                 System.debug('This is block just for closing the class');
            }      
          
 
     if(!cusermap.isEmpty()) {
      update cusermap.values();
     }

        
   }
}

and trigger is 

trigger CaseUserHelperTrigger on Case(before insert,before update){     

        CaseUserHelper.updateCaseStage(Trigger.new);
}

could you please anyone help

Thanks In Advance
sagarika bsagarika b
Hi Chanbasha nbsk,
 
There is an example in the Visualforce Developer's Guide:

1. http://www.salesforce.com/us/developer/docs/pages/index.htm
2. Go to Visualforce Controllers -> Custom Controllers and Controller Extensions -> Testing Custom Controllers and Extensions

In short, to write test methods for controllers, you want to write test scenarios to validate your controller's behavior. 
You can instantiate the controller class just like any other class or object. Then you can 'set' and 'get the property values to start testing the 
test scenarios. And you can also execute the action methods which return a PageReference and check to see if the proper Page or URL is returned. 


We hope it helps you,

Please mark it as best answer if it resolved the issue.​

BestRegrads
Sagarika