-1

I have tried so many ways to check the code coverage from "apex test execution", developer console & tests tab in console but no luck.

this is my test class

@isTest
public class CaseUserHelperTest{

    static testMethod void helperMethod(){

           // List<String> skillSetList = new List<String>();
           Datetime myDate = Datetime.newInstance(2017, 24, 3, 10, 50, 0);
           Datetime myDate1 = Datetime.newInstance(2017, 24, 3, 9, 50, 0);

            Test.startTest();         

            //Case_User__c cuser1 = new Case_User__c(AvailableUsers__c='00546000000hZUx',Timings__c='2:00PM-11:00PM', SkillSet__c ='Product Support',Availability__c = TRUE,Last_Case_Allocated_Time__c = myDate );
            Case_User__c cuser1 = new Case_User__c(AvailableUsers__c='00546000000hZWK',Timings__c='2:00PM-11:00PM', SkillSet__c ='Product Support',Availability__c = TRUE,Last_Case_Allocated_Time__c = myDate1 );
            Case_User__c cuser2 = new Case_User__c(AvailableUsers__c='00546000000hZUx',Timings__c='2:00PM-11:00PM', SkillSet__c ='Product Support',Availability__c = TRUE,Last_Case_Allocated_Time__c = myDate );
            insert cuser1;
            insert cUser2;

            Case cas = new Case(Status ='New', Priority = 'Medium', Origin = 'Email',CaseRelatedTo__c='Product Support');
            insert cas;


            Test.stopTest();
            cas = [Select OwnerId From Case where Id=:cas.id];
            System.assertEquals(cas.OwnerId, '00546000000hZWK');


  }

}

This is my apex class

public class Helper {

    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               

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

     if(!cusermap.isEmpty()) {
      update cusermap.values();
     }


   }
}

this is trigger

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

        CaseUserHelper.updateCaseStage(Trigger.new);

}

This is my test class

@isTest

public class CaseUserHelperTest{

    static testMethod void helperMethod(){

           // List<String> skillSetList = new List<String>();
           Datetime myDate = Datetime.newInstance(2017, 24, 3, 10, 50, 0);
           Datetime myDate1 = Datetime.newInstance(2017, 24, 3, 9, 50, 0);

            Test.startTest();         

            //Case_User__c cuser1 = new Case_User__c(AvailableUsers__c='00546000000hZUx',Timings__c='2:00PM-11:00PM', SkillSet__c ='Product Support',Availability__c = TRUE,Last_Case_Allocated_Time__c = myDate );
            Case_User__c cuser1 = new Case_User__c(AvailableUsers__c='00546000000hZWK',Timings__c='2:00PM-11:00PM', SkillSet__c ='Product Support',Availability__c = TRUE,Last_Case_Allocated_Time__c = myDate1 );
            Case_User__c cuser2 = new Case_User__c(AvailableUsers__c='00546000000hZUx',Timings__c='2:00PM-11:00PM', SkillSet__c ='Product Support',Availability__c = TRUE,Last_Case_Allocated_Time__c = myDate );
            insert cuser1;
            insert cUser2;

            Case cas = new Case(Status ='New', Priority = 'Medium', Origin = 'Email',CaseRelatedTo__c='Product Support');
            insert cas;


            Test.stopTest();
            cas = [Select OwnerId From Case where Id=:cas.id];
            System.assertEquals(cas.OwnerId, '00546000000hZWK');


  }

}
7
  • 1
    You haven't invoked your controller Commented Mar 24, 2017 at 5:10
  • 1
    Try to avoid hard coding ids.
    – Reshma
    Commented Mar 24, 2017 at 5:17
  • 1
    @devang Code coverage is shown for the Apex Class/Trigger. If you are not creating reference of your controller and then invoking it's method then how come you'll know about their code coverage. In case you've written Trigger then no need to create any reference. You can see it's code coverage directly. Commented Mar 24, 2017 at 5:23
  • I created a trigger for calling the apex class on Case Object
    – devang
    Commented Mar 24, 2017 at 5:32
  • Post your trigger and apex class. Commented Mar 24, 2017 at 5:34

1 Answer 1

0

If you are using some data from your org then try using "@isTest(SeeAllData=true)"

@isTest(SeeAllData=true)
           public class CaseUserHelperTest{

            static testMethod void helperMethod(){
            //List<String> skillSetList = new List<String>();
            Datetime myDate = Datetime.newInstance(2017, 24, 3, 10, 50, 0);
            Datetime myDate1 = Datetime.newInstance(2017, 24, 3, 9, 50, 0);
            Test.startTest();         
            //Case_User__c cuser1 = new Case_User__c(AvailableUsers__c='00546000000hZUx',Timings__c='2:00PM-11:00PM', SkillSet__c ='Product Support',Availability__c = TRUE,Last_Case_Allocated_Time__c = myDate );
            Case_User__c cuser1 = new Case_User__c(AvailableUsers__c='00546000000hZWK',Timings__c='2:00PM-11:00PM', SkillSet__c ='Product Support',Availability__c = TRUE,Last_Case_Allocated_Time__c = myDate1 );
            Case_User__c cuser2 = new Case_User__c(AvailableUsers__c='00546000000hZUx',Timings__c='2:00PM-11:00PM', SkillSet__c ='Product Support',Availability__c = TRUE,Last_Case_Allocated_Time__c = myDate );
            insert cuser1;
            insert cUser2;
            Case cas = new Case(Status ='New', Priority = 'Medium', Origin = 'Email',CaseRelatedTo__c='Product Support');
            insert cas;
            Test.stopTest();
            cas = [Select OwnerId From Case where Id=:cas.id];
            System.assertEquals(cas.OwnerId, '00546000000hZWK');
  }

}

This will help you but your style of writing test classes are not in the best practices . Instead of taking records from org(AvailableUsers__c='00546000000hZUx) try to create dummy records in your test class for testing purpose. Please mark my answer as a solution if it is helpful.

Regards,
Akshay

Not the answer you're looking for? Browse other questions tagged .