function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFHelpMePleaseSFHelpMePlease 

apex testing can't get to 75%, how can I test the missing lines

I am at 71%, 4 lines of code cannot be run in the test for some reason.
When I test myself in Salesforce it works (those lines of code are running).
How can I get these lines of code to run?
1. Lines not running, in second for loop
   {   nextId=Integer.Valueof(c.next_id__c);     }
2. third for loop
            btnRecord.next_id__c = newid + 1;
            btnRecord.last_id__c = newId;
            btnRecord.last_assigned_starting_id__c = nextId;
            btnRecord.last_assigned_ending_id__c = newId;
trigger getNextId on tracking__c (before insert, before update) {
    Integer newId;
    Integer lastId;
    Integer nextId;
    newId=0;
    lastId=0;
    nextId =0;
    //add the total accounts to the last_id
    for (tracking__c bt: Trigger.new) {
        //get the next id
        List<tracking_next_id__c> btnxtid = [SELECT  next_id__c FROM tracking_next_id__c];
        for (tracking_next_id__c c : btnxtid )
        {
           nextId=Integer.Valueof(c.next_id__c);
        }
        newId = Integer.Valueof(bt.total_account__c) + nextId;
        bt.starting_id__c = nextId;
        bt.ending_id__c = newId;
       
        tracking_next_id__c[] nextIdToUpdate = [SELECT last_id__c, next_id__c, last_assigned_starting_id__c, last_assigned_ending_id__c FROM tracking_next_id__c];
        for(tracking_next_id__c btnRecord : nextIdToUpdate ){
            btnRecord.next_id__c = newid + 1;
            btnRecord.last_id__c = newId;
            btnRecord.last_assigned_starting_id__c = nextId;
            btnRecord.last_assigned_ending_id__c = newId;
        }
        update nextIdToUpdate ;
   }
}
----------------------------------------------------------------------------------
--Test
----------------------------------------------------------------------------------
@isTest
private class getNextIdTest {
    static testMethod void validateOnInsert(){
Test.startTest();
    tracking__c b = new tracking__c(total_account__c=Integer.Valueof(99));
    System.debug('before insert : ' + b.total_account__c);
    insert b;
    System.debug('after insert : ' + b.total_account__c);
    List<tracking__c> customObjectList = [SELECT total_account__c FROM tracking__c ];
    for(bid_tracking__c ont : customObjectList){
      ont.total_account__c = 5;
      }
      update customObjectList;
Test.stopTest();
}
}
 
Best Answer chosen by SFHelpMePlease
SFHelpMePleaseSFHelpMePlease
Thank you!!!! I added @isTest(SeeAllData=true) and it moved to 100%

All Answers

Balayesu ChilakalapudiBalayesu Chilakalapudi
Try like this,
@isTest
private class getNextIdTest {
    static testMethod void validateOnInsert(){
Test.startTest();   
    tracking__c b = new tracking__c(total_account__c=Integer.Valueof(99));
    System.debug('before insert : ' + b.total_account__c);
    insert b;    
    tracking_next_id__c tracker=new tracking_next_id__c();    
    tracker.next_id__c =b.Id;  
    tracker.last_id__c=null;  // try giving some valid Id's instead of null for other too 
    tracker.last_assigned_starting_id__c=null;   
    tracker.last_assigned_ending_id__c =null;   
    insert tracker;
    System.debug('after insert : ' + b.total_account__c);
    List<tracking__c> customObjectList = [SELECT total_account__c FROM tracking__c ];
    for(bid_tracking__c ont : customObjectList){
      ont.total_account__c = 5;
    }
    update customObjectList;
  Test.stopTest();
}
}

Let us know if it helps,

Thanks,
Balayesu
SFHelpMePleaseSFHelpMePlease
Thank you so much for reviewing!! I ran and still shows 71%.
Balayesu ChilakalapudiBalayesu Chilakalapudi
You have two options,
1) change your @isTest like,
@isTest(SeeAllData=true)
OR
2) Make sure that tracking__ctracking_next_id__c are inserted successfully from your test class without any error.
  In your debug log search like System.DML for finding any DML errors.


 
SFHelpMePleaseSFHelpMePlease
Thank you!!!! I added @isTest(SeeAllData=true) and it moved to 100%
This was selected as the best answer