0

I have an extension for Case object but it does not go to my extension method. Is there anything I forgot to include in my code

This is a snippet of test class

>  test.startTest();
        ApexPages.StandardController sc = new ApexPages.StandardController(Case_test[0]);
        CaseExtension CaseExt = new CaseExtension(sc);

        test.stopTest();

This is the extension

public class CaseExtension {
  ApexPages.StandardController c;
  public CaseExtension(ApexPages.StandardController controller) {
    c = controller;
  }
  public PageReference updateCase() {

    Case caseRecord = (Case)c.getRecord();
    List<Case> caseForUpdate = new list<Case>();

    for(Case c: [SELECT Id,Changed_into_In_progress__c ,viewed_by_Handler_TimeStamp__c,viewed_by_Handler__c, OwnerId, Case_Brand_Handler__c, Transaction_Group__c, Transaction_Item__c, Status 
                        FROM Case WHERE Id =:  caseRecord.Id ]){

         //system.assertequals(c,null);
        if(c.viewed_by_Handler_TimeStamp__c == NULL && UserInfo.getUserId() == c.OwnerId){ 
         if(c.Transaction_Group__c == 'Sample' && c.Transaction_Item__c == 'Sample' && c.Changed_into_In_progress__c == NULL){

             c.viewed_by_Handler__c = TRUE;
             c.viewed_by_Handler_TimeStamp__c = System.now();

             caseForUpdate.add(c);
         }
       }       
    }

    if( caseForUpdate.size() > 0 ){
        update caseForUpdate;
    }

    return null;  

  }
}
1
  • 1
    Pro tip: if( caseForUpdate.size() > 0 ){ is superfluous and can be removed
    – cropredy
    Jan 9, 2018 at 6:24

1 Answer 1

2

You need to actually call the updateCase() method. There's no user here clicking buttons or taking actions, you have to simulate what that user might do (if you're aiming for an integration style test) or check the validity of individual methods if you want to do unit tests. Both are viable, both benefit from having the other, but it's up to you to decide how you want to do it.

It looks as though you're initialising the standard controller with a record, so assuming you've setup all the data correctly, you just need to add this before your call to stopTest():

CaseExt.updateCase();

That would get you coverage, but is it a good test? No. You want to use the various System class assertion methods to confirm your code is working as you expect it to, e.g.

Case updatedCase = [select Id, viewed_by_Handler__c from Case where Id = : Case_test[0].Id limit 1];
System.assertEquals(true, updatedCase.viewed_by_Handler__c, 'viewed_by_Handler__c should have been set to true');

What this method does is check that viewed_by_Handler__c is the same as true, and if it's not the test will fail with the message provided. This isn't a great example and I'm making a few guesses based on your code, but it should give you the idea!

0

You must log in to answer this question.

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