+ Start a Discussion
Rajesh SFDCRajesh SFDC 

test calss how to get above 75% in apex trigger?

how to increase code coverage now i am geeting a 55% in apex trigger...
trigger test on Case (before update )
{
  Map<string, Schema.SobjectField> caseFields = Schema.SObjectType.Case.fields.getMap();

  case caseold=trigger.old[0];
  For (Case cs : Trigger.new)
  {
    for (string fieldName : caseFields.keySet())
     {
       
        if ( cs.get(fieldName) != Trigger.oldMap.get(cs.id).get(fieldName))
        {
           // cs.Track_status_history__c = 'Field ' + fieldName + ' changed from ' + string.valueOf(Trigger.oldMap.get(cs.id).get(fieldName)) + ' to '  +string.valueOf(cs.get(fieldName));
             string oldvalue = string.valueOf(caseold.get(fieldName));------------->doesnot executing
            
             string newvalue= string.valueOf(cs.get(fieldName));------------->doesnot executing
           
             cs.Track_status_history__c = 'Changed '+fieldName +' From '+oldvalue +' to '+ newvalue;
         
             break;
          }   
       }
  }
}
=============================================
@isTest
private class Testcasehistory
{

   static testMethod void updatefields()
    {
     list<Case> cslist=new list<case>();
     case cs=new case();
     cs.Origin='Web';
     cs.Reason='other';
     cs.Priority='Medium';
     cs.Status='New';
     cs.Type='Other';
     cslist.add(cs);
     test.startTest();
      insert cslist;
    
     Case updcase = [SELECT Id,Origin,Reason,Priority,Status,Type  FROM Case WHERE Id=:cs.Id];
     update updcase;
     test.stopTest();
    }
}=====================
Best Answer chosen by Rajesh SFDC
hitesh90hitesh90
Hello Rajesh,

You have to make some change in your test class in update statement.
see following code.

Test class:
@isTest
private class Testcasehistory{
    static testMethod void updatefields(){
        list<Case> cslist=new list<case>();
        case cs=new case();
        cs.Origin='Web';
        cs.Reason='other';
        cs.Priority='Medium';
        cs.Status='New';
        cs.Type='Other';
        cslist.add(cs);
        test.startTest();
        insert cslist;
        
        cs.Origin='Email';
        update cslist;
        test.stopTest();
    }
}

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
Email :- hiteshpatel.aspl@gmail.com
My Blog:- http://mrjavascript.blogspot.in

All Answers

Ramu_SFDCRamu_SFDC
Hi, Follow the guidelines as mentioned in the below article

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_best_practices.htm
Abdel-Ali BOULIKAAbdel-Ali BOULIKA
Hi,
To cover the uncovered lines, you just need to update one field just before doing the "update updcase;" in your unit test.

You can try by doing something like :
Case updcase = [SELECT Id,Origin,Reason,Priority,Status,Type  FROM Case WHERE Id=:cs.Id];
updcase.Origin = 'Phone';// Initial value was "Web", so here just set any value different from the initial one
update updcase;

hitesh90hitesh90
Hello Rajesh,

You have to make some change in your test class in update statement.
see following code.

Test class:
@isTest
private class Testcasehistory{
    static testMethod void updatefields(){
        list<Case> cslist=new list<case>();
        case cs=new case();
        cs.Origin='Web';
        cs.Reason='other';
        cs.Priority='Medium';
        cs.Status='New';
        cs.Type='Other';
        cslist.add(cs);
        test.startTest();
        insert cslist;
        
        cs.Origin='Email';
        update cslist;
        test.stopTest();
    }
}

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
Email :- hiteshpatel.aspl@gmail.com
My Blog:- http://mrjavascript.blogspot.in
This was selected as the best answer
Rajesh SFDCRajesh SFDC
THANKS ITS 100% CODE