0

This is the main class... and i am trying to cover the code in test class....

public static void setProgress(List<AgentWork> newAgentWorks, Map<Id,AgentWork> oldAgentWorks, Boolean isInsert){

        Set<Id> caseIds = new Set<Id>();
        List<Case> casesToUpdate = new List<Case>();

        for (AgentWork newAgentWork :newAgentWorks){

            if (newAgentWork.Status == 'Open'  && (isInsert || oldAgentWorks.get(newAgentWork.Id).Status != 'Open')){
                caseIds.add(newAgentWork.WorkItemId);
            }
        }

        if (caseIds.size() > 0){

            casesToUpdate = [Select Id, Status, Owner_Changed 
                             From Case 
                             Where Id IN :caseIds and Status = 'New'];

            for (Case c :casesToUpdate){

                c.Status = 'Work in Progress';
                if(c.Owner_Changed== null){
                    c.Owner_Changed= system.now();
                }
            }

            update casesToUpdate;
        }
    }

Test class:

  public class Model_Test {

        public static List<CaseHistory> list_CasesHistory;   

        static testMethod void WorkTest() {

            setup();

            Case c1 = new Case();
            c1.Origin='Email';
            c1.Status = 'New';
            c1.Owner_Changed_from_Queue_to_Agent__c = system.now();
            insert c1;

            Case c2 = new Case();
            c2.origin = 'Phone';
            c2.Status = 'New';
            c1.Owner_Changed_from_Queue_to_Agent__c = system.now();
            insert c2;

            List<Case>lstCase = new List<Case>();
            lstCase.add(c1);
            lstCase.add(c2);

            AgentWork awork = new AgentWork();
            awork.WorkItemId = c1.Id;
            awork.UserId = UserInfo.getuserid();
            awork.ServiceChannelId = SChannel.Id;
            insert awork;        

            AgentWork awork1 = new AgentWork();
            awork1.WorkItemId = c2.Id;
            awork1.UserId = UserInfo.getuserid();
            awork1.ServiceChannelId = SChannel.Id;
            insert awork1;

            List<AgentWork>lstAgent = new List<AgentWork>();
            lstAgent.add(awork);
            lstAgent.add(awork1);

            Map<Id,AgentWork> mapAgent = new Map<Id,AgentWork>();
            mapAgent.put(awork.Id,awork);
            Boolean isInsert = true;
            Model_Test agent = new Model_Test ();

            Test.starttest();
            Model_Test.setWorkInProgress(lstAgent,mapAgent,isInsert);
            Test.stoptest();         

        }
    }

The if(caseIds.size() > 0){} within the Test Class is not being covered and as a part of the test code coverage. Need help on understanding on how the lines within the stated if condition can be covered to increase the test code coverage.

1
  • is 'setWorkInProgress' here the right method? Or is it 'setProgress' ?
    – utm
    Jul 3 '18 at 9:41
1

Its because,itsnot entering inside the if statement.If it enters inside the if statement then only you will get value for "caseIds"

If you are writing trigger on Update case below code will work.

  Case c2 = new Case();
  c2.origin = 'Phone';
 c2.Status='New';
 c1.Owner_Changed_from_Queue_to_Agent__c = system.now(); 
 insert c1;
 c1.Status = 'Open';  
 update c1;

Please start test class with @isTest Annotation

Test class best practices:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm

Thinking About it:

If you are calling "setProgress" method inside the trigger (why because I can see other variable "Insert".If you do necessary "dml" operation on trigger, automatically this method (setProgress) will cover and also if thats the case avoid soql query in this method.

3
  • yes..this is a handler class written for the trigger....
    – kiran
    Jul 3 '18 at 10:24
  • I already answered your question.In that case just cover trigger (based on DML operation),this method will also cover Jul 3 '18 at 10:27
  • Its okay kiran .. :) Jul 3 '18 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.