-1

So I hate writing test classes with my soul. Any resources so I don't have to come here all the time?

Anyhow, any help is appreciated.

Trigger:

trigger CaseBeforeTrigger on Case (before update) {
 Case_TriggerHandler handler = new Case_TriggerHandler(Trigger.new, Trigger.newMap, Trigger.old, Trigger.oldMap, Trigger.operationType);
        handler.beforeupdate();
}

Apex class referenced in the trigger:

Public with sharing class Case_TriggerHandler extends TriggerHandler
    {
    public Case_TriggerHandler(List<Case> newList, Map<Id,case> newMap, List<case> oldList, Map<Id, Case> oldMap, TriggerOperation type){
        super(newList, newMap, oldList, oldMap, type);
        System.debug('Case Trigger handler created');
        beforeUpdate();
    }
public void beforeupdate(){
      Date Threedys;
      System.debug('On before update called from Case Trigger Handler');
        for (Case C : (list<case>) Trigger.new) 
        {
            List<Case> CH = [select id, status, lastmodifieddate from case where id =:c.Id];
            Case[] ul = ch;
            Date clcdt;
            if(!ul.isempty()){
            clcdt = ul[0].LastModifiedDate.date();
            }
            else
                clcdt = date.newinstance(1900,1,1);
            Datetime dt = DateTime.newInstance(c.NextBusinessDay__c, Time.newInstance(0, 0, 0, 0));
            String dayOfWeek=dt.format('E');
            If(!ul.isempty() && c.NextBusinessDay__c != date.newinstance(1900,1,7) && c.NextBusinessDay__c >=date.today()&& ul[0].status == 'Resolved'){
                if(dayOfWeek =='Mon'){
                    Threedys=c.NextBusinessDay__c.adddays(2);
                }
                Else if(dayOfWeek == 'Tue'){
                    Threedys=c.NextBusinessDay__c.adddays(2);
                }
                else if(dayOfWeek == 'Wed'){
                    Threedys=c.NextBusinessDay__c.adddays(4);
                }
                else If(dayOfWeek=='Thu'){
                    Threedys=c.NextBusinessDay__c.adddays(4);
                }
                else if(dayOfWeek == 'Fri'){
                    Threedys=c.NextBusinessDay__c.adddays(4);
                }
                else If(dayOfWeek == 'Sat'){
                    Threedys=c.NextBusinessDay__c.adddays(3);
                }
                else if(dayOfWeek == 'Sun'){
                    Threedys=c.NextBusinessDay__c.adddays(2);
                }
             system.debug(Threedys);
            }
            else if(!ul.IsEmpty()){  
            string nwdt = ul[0].LastModifiedDate.format('E');
            ul.sort();
            If(nwdt =='Mon' && ul[0].status == 'Resolved'){
            Threedys=(clcdt.adddays(3));
            }
           else If(nwdt =='Tue' && ul[0].status == 'Resolved'){
            Threedys=(clcdt.adddays(3));
            }
           Else If(nwdt =='Wed' && ul[0].status == 'Resolved'){
            Threedys=(clcdt.adddays(5)); 
            }
           else If(nwdt =='Thu' && ul[0].status =='Resolved'
                  ){
            Threedys=(clcdt.adddays(5));
            }
           else If(nwdt  =='Fri' && ul[0].status == 'Resolved'){
            Threedys=(clcdt.adddays(5));
           }
          else If(nwdt =='Sat' && ul[0].status == 'Resolved'){
            Threedys=(clcdt.adddays(4));
            }
           else if(nwdt =='Sun' && ul[0].status == 'Resolved'){
            Threedys=(clcdt.adddays(3));
        }
             c.DateToClose__c = threedys;

           }
        }
    system.debug(Threedys);
}
    }

Handler:

public virtual with sharing class TriggerHandler
{   public List<SObject> newTrigger {get; set;}
    public List<SObject> oldTrigger {get; set;}
    public Map<Id, SObject> newMap {get; set;}
    public Map<Id, SObject> oldMap {get; set;}
    public TriggerOperation type {get; set;}
 
    public TriggerHandler(List<SObject> newTrigger, Map<Id, SObject> newMap, List<SObject> oldTrigger, Map<Id, SObject> oldMap, TriggerOperation type){
          this.newTrigger = newTrigger; this.newMap = newMap; this.oldTrigger = oldTrigger; this.oldMap = oldMap; this.type = type;}

}

Attempt at a test class. I can only ever get to 68 percent code coverage.

@isTest
private class CaseBeforeTrigger_Test{
    static testMethod void myUnitTest() {
       list<case> ca = new list<case>();
       case c =  new case();
       c.Subject = 'Test Case';
       c.Status = 'Resolved';
       c.Description = 'Test Case Description';
       c.SuppliedEmail = '[email protected]';
       c.Domain__c = 'Lemon Law Aid Inc';
       c.SuppliedName = 'Kermit TheFrog';
       c.lastmodifieddate = date.newinstance(2020,8,3);
       c.CreatedDate = date.newinstance(2020,8,3);
        
       ca.add(c);
        
       case c1 = new case();
       c1.Subject = 'Test Case1';
       c1.Status = 'Resolved';
       c1.Description = 'Test Case Description1';
       c1.SuppliedEmail = '[email protected]';
       c1.Domain__c = 'Lemon Law Aid Inc1';
       c1.SuppliedName = 'Kermit TheFrog1';
       c1.lastmodifieddate = date.newinstance(2020,8,2);
       c1.CreatedDate = date.newinstance(2020,8,2);
        
       ca.add(c1);
        
       case c2 = new case();
       c2.Subject = 'Test Case2';
       c2.Status = 'Resolved';
       c2.Description = 'Test Case Description2';
       c2.SuppliedEmail = '[email protected]';
       c2.Domain__c = 'Lemon Law Aid Inc2';
       c2.SuppliedName = 'Kermit TheFrog2';
       c2.lastmodifieddate = date.newinstance(2020,8,1);
       c2.CreatedDate = date.newinstance(2020,8,1);
        
       ca.add(c2);
        
       case c3 = new case();
       c3.Subject = 'Test Case3';
       c3.Status = 'Resolved';
       c3.Description = 'Test Case Description3';
       c3.SuppliedEmail = '[email protected]';
       c3.Domain__c = 'Lemon Law Aid Inc3';
       c3.SuppliedName = 'Kermit TheFrog3';
       c3.lastmodifieddate = date.newinstance(2020,7,31);
       c3.CreatedDate = date.newinstance(2020,7,31);
        
        ca.add(c3);
        
       case c4 = new case();
       c4.Subject = 'Test Case4';
       c4.Status = 'Resolved';
       c4.Description = 'Test Case Description4';
       c4.SuppliedEmail = '[email protected]';
       c4.Domain__c = 'Lemon Law Aid Inc4';
       c4.SuppliedName = 'Kermit TheFrog4';
       c4.lastmodifieddate = date.newinstance(2020,7,30);
       c4.CreatedDate = date.newinstance(2020,7,30);
        
       ca.add(c4);
        
       case c5 = new case();
       c5.Subject = 'Test Case5';
       c5.Status = 'Resolved';
       c5.Description = 'Test Case Description5';
       c5.SuppliedEmail = '[email protected]';
       c5.Domain__c = 'Lemon Law Aid Inc5';
       c5.SuppliedName = 'Kermit TheFrog5';
       c5.lastmodifieddate = date.newinstance(2020,7,29);
       c5.CreatedDate = date.newinstance(2020,7,29);
        
       ca.add(c5);
        
       case c6 = new case();
       c6.Subject = 'Test Case6';
       c6.Status = 'Resolved';
       c6.Description = 'Test Case Description6';
       c6.SuppliedEmail = '[email protected]';
       c6.Domain__c = 'Lemon Law Aid Inc6';
       c6.SuppliedName = 'Kermit TheFrog6';
       c6.lastmodifieddate = date.newinstance(2020,7,28);
       c6.CreatedDate = date.newinstance(2020,7,28);
        
       ca.add(c6);
        
       insert ca;
       update ca;
    }
}

Again thanks to anyone who has the time to help.

I know the code coverage is related to the If statement. But how do you test out both possibilities of the If?

3
  • Can you fix your formatting? Some parts are quite hard to read.
    – adriancg
    Commented Aug 5, 2020 at 0:06
  • Make sure that you add some system.assert() to your test classes to make sure the code is actually functioning as expected. Check out the testing best practices article.
    – Zach
    Commented Aug 6, 2020 at 20:02
  • thank you so much for your feedback! Commented Aug 7, 2020 at 19:19

1 Answer 1

2

There's nothing wrong with your unit test, as far as I can tell. There are many problems wrong with your code.

Public with sharing class Case_TriggerHandler extends TriggerHandler { 
    public Case_TriggerHandler(List<Case> newList, Map<Id,case> newMap, List<Case> oldList, Map<Id, Case> oldMap, TriggerOperation type){ 
        super(newList, newMap, oldList, oldMap, type); 
        System.debug('Case Trigger handler created'); 
        /////////////////////////////////////////////////////////////////
        // Do NOT call this method here. Your trigger already calls it //
        /////////////////////////////////////////////////////////////////
        // beforeUpdate(); 
    } 
    public void beforeupdate(){ 
        // This wasn't a necessary variable.
        // Date Threedys; 
        // Check your TriggerHandler framework for the correct variable name to use. Do not use Trigger.new here.
        // Avoid one character variable names. You're given a limit of 40 characters for a reason.
        for (Case C : (list<case>) newList) { 
            // Do not query inside a loop. Also, You already have these values in memory. They came from Trigger.new.
            // List<case> CH = [select id, status, lastmodifieddate from case where id =:c.Id]; 
            
            // Useless variable, may as well call it CH.
            // Case[] ul = ch; 
            
            
            // This if statement would always be true.
            // if(!ul.isempty()){ 
            // Also, LastModifiedDate hasn't updated yet. Do you mean 'now'?
            // Date clcdt = C.LastModifiedDate.date();
            Date clcdt = DateTime.now();
            // clcdt? That's a seriously bad name. I've written code for 32+ years, I have no idea what you're trying to say.

            // Avoid null pointer exception; let's go on if NextBusinessDay__c is null.
            // Also, looks like everything needs to be resolved, so skip if not.
            if(c.NextBusinessDay__c == null || c.Status != 'Resolved') {
                continue;
            }
            
            // Following code is impossible to cover in original IF statement because it would never be false. 
            // Also, do NOT omit curly braces because you can. This leads to logic errors, especially for novice developers.
            //} else 
            //    clcdt = date.newinstance(1900,1,1); 
            // dt? No, don't call it dt. Use the variable name to describe what the variable holds. What is dt supposed to be?
            Datetime dt = DateTime.newInstance(c.NextBusinessDay__c, Time.newInstance(0, 0, 0, 0)); 
            // See, this is a GOOD name. Do all the names like this one.
            String dayOfWeek=dt.format('E').toUpperCase(); 
            // Combine all the extra logic for dates.
            // We add one day if date is in the past.
            Integer dateOffset = c.NextBusinessDay__c != date.newinstance(1900,1,7) && c.NextBusinessDay__c >= Date.today()? 0: 1;
            // !ul.isEmpty() is always true. Possible Null Pointer Exception errors abound here.
            // Also, this is a great time to use a switch statement.
            // Removed rest of code for brevity.
            switch on dayOFWeek {
                when 'MON','TUE','SUN' {
                    dateOffset += 2;
                }
                when 'WED','THU','FRI' {
                    dateOffset += 4;
                }
                when 'SAT' {
                    dateOffset += 3;
                }
            }
            c.DateToClose__c = c.NextBusinessDay__c.addDays(dateOffset);
        }
    }
}
1
  • I appreciate the help. I only "kind of" know what I'm doing. ha ha. I'm figuring it out as I go. I appreciate all the help. Thanks a million! Commented Aug 5, 2020 at 15:08

You must log in to answer this question.

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