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
Eric Blaxton 11Eric Blaxton 11 

Help with Test Class. 6 lines aren't covered.

Hi and thanks for help in advance.

I need help to get better coverage.  I am at 73%.  There's about 6 lines i need help with.

///These 6 lines in the Apex Class below  arent covered.
                            cs.SendtoTMW__C = false;
                            result= 'There is no valid TMW Surrogate ID for this case';
                            return result;

                        }
                    }else{
                        cs.SendtoTMW__C = false;
                        result= 'There is no valid TMW Surrogate ID for this case';
                        return result; 


Apex Class:

public with sharing class SendtoTMW {
    
    @AuraEnabled  
    public static String getCurCase(Id caseId)
    {
        //Query current user profile id
        String result;
        Case cs =  [select id,SendtoTMW__c,SendtoTMWPerm__c,SFtoTMWSent__c,Origin,AccountId
                    FROM Case WHERE id = :caseId];
        System.debug('#### cs = '+ cs);
        if(cs != null){
            
            if (cs.AccountId == null) {
                cs.SendtoTMW__C = false;
                result= 'Order cannot be submitted to TMW.  Please enter an Account on the Case.';
                return result;
            } else{
                // Check if TMW surrogate ID exists in translation table
                //Id surrogateIds = '';
                //var surrogateIds = sforce.connection.query("SELECT Surrogate_ID__c FROM Translation_Table__c where IntegratedSite__c = '{!Case.AccountId}'"); //Commented this line out because it if user did not have rights to TT then would fail. This next line runs for all.
                List<Translation_Table__c> listTranslationTable = TMWSendOrderService.GetSurrogates(cs.AccountId);
                // I need to pass the case id here.  I'm getting a null return value.  This is what passes it to TMWSendOrderService and sends to TMW 
               // csid = TMWSendOrderService.SendOrderToTmw(cs.Id);
                System.debug('#### list trans table = '+ listTranslationTable);
                if(!listTranslationTable.isEmpty()){
                    if(null !=  listTranslationTable[0].Surrogate_ID__c){
                        String surrogateId = listTranslationTable[0].Surrogate_ID__c;
                        if(surrogateId.substring(0, 3) != 'TMW'){
///These 6 lines arent covered.
                            cs.SendtoTMW__C = false;
                            result= 'There is no valid TMW Surrogate ID for this case';
                            return result;

                        }
                    }else{
                        cs.SendtoTMW__C = false;
                        result= 'There is no valid TMW Surrogate ID for this case';
                        return result; 

                    }
                    
                }else{
                    cs.SendtoTMW__C = false;
                    result = 'There is no valid TMW Surrogate ID for this case';
                    return result;
                }
                if ((cs.Origin == 'TMWUI To Case' || cs.Origin == 'TMWFIX To Case' || cs.Origin == 'TMWFRCST To Case' || cs.Origin == 'TMWCarrierHUB To Case')) {
                    result= 'Order already submitted to or received from TMW. All updates are managed in TMW interface';
                    return result;
                }
                
                if ((cs.Origin != 'TMWUI To Case')) {
                    if (cs.Origin != 'TMWFIX To Case') {
                        if (cs.Origin != 'TMWFRCST To Case') {
                            if (cs.Origin != 'TMWCarrierHUB To Case') {
                                if (cs.SendtoTMW__C == true) {
                                    cs.SendtoTMW__C = false;
                                    result = 'Order already submitted to or received from TMW. All updates are managed in TMW interface.';
                                    return result;
                                }
                            }
                        }
                    }
                }
                Profile pr =  [Select Id, Name from Profile Where Id =: UserInfo.getProfileId() LIMIT 1]; 
                if(pr.Name == 'Transportation Mgmt'|| pr.Name == 'Transportation Team' || pr.Name == 'System Administrator')
                {
                    
                   System.debug('#### profile name = '+ pr);
                   System.debug('### test is running' + cs.SendtoTMW__C);
                    
                    if(Test.isRunningTest()){
                        System.debug('### test is running ' + cs.SendtoTMW__C);
                        cs.SendtoTMW__C = true;
                    }
                    
                    if (cs.SendtoTMW__C == false) {
                        
                        try
                        {
                            result =TMWSendOrderService.SendToTmw(cs.Id);
                           // System.debug('### send to tmw ' + cs.SendtoTMW__C);
                            return result;
                            
                            
                        }
                        catch(Exception ex)
                        {
                            result = ex.getMessage();
                           // System.debug('### send to tmw catch ' + cs.SendtoTMW__C);
                            return result;                            
                        }
                    }
                }                
            }           
        }else{            
        }        
        return null;
    }    
}

Test Class

@isTest
private class SendtoTMWTest 
{    
    // create test case
    @testSetup
    static void setupTestData(){
        test.startTest();
        Account ac=new Account(name='Test Account');
        Insert ac;
        Translation_Table__c translation_table_Obj = new Translation_Table__c(Integration_Status__c = false, IntegratedSite__c= ac.id, 
                                                                              Surrogate_ID__c='TMW-Test123');
        Insert translation_table_Obj; 
        test.stopTest();
    }
    
    //create Account and TT entry 
    @isTest
    static void getCurCase(){
        Account ac=new Account(name='Test Account');
        Insert ac;
        List<Translation_Table__c> translation_table_Obj  =  [SELECT Integration_Status__c, Name from Translation_Table__c];
        System.assertEquals(true,translation_table_Obj.size()>0);
        TMWSendOrderService obj01 = new TMWSendOrderService();
        TMWSendOrderService.OrderResponse obj11 = new TMWSendOrderService.OrderResponse();
        TMWSendOrderService.FuelOrdered obj21 = new TMWSendOrderService.FuelOrdered();
        TMWSendOrderService.TmwOrder obj31 = new TMWSendOrderService.TmwOrder();
        
        
        Case caseObj = new Case(accountId = ac.id, status='Open', 
                                Fuel_Order_Status__c = 'Pending',  
                                Origin = 'Phone',
                                RQ_Delivery_Date__c=system.Today(), 
                                RQ_Delivery_Timeframe__c='9am-3pm',
                                Description='testing fuel order');
        // Add all required field here
        insert  caseObj;
        SendtoTMW.getCurCase(caseObj.id);
        caseObj.AccountId = null;
        update caseObj;
        SendtoTMW.getCurCase(caseObj.id);
        
        caseObj.AccountId = ac.id;
        update caseObj;
        for(Translation_Table__c trans: translation_table_Obj){
            trans.IntegratedSite__c = ac.Id;
        }
        update  translation_table_Obj;
        SendtoTMW.getCurCase(caseObj.id);
       
        
        caseObj.SendtoTMW__c = true;
        update CaseObj;
        SendtoTMW.getCurCase(caseObj.id);
        
        caseObj.Origin ='TMWUI To Case';
        update caseObj;
        SendtoTMW.getCurCase(caseObj.id);
        
        caseObj.SendtoTMW__c = true;
        update CaseObj;
        SendtoTMW.getCurCase(caseObj.id);
        
         
        for(Translation_Table__c trans: translation_table_Obj){
            trans.IntegratedSite__c = ac.Id;
            trans.Surrogate_ID__c = null;
        }
        update  translation_table_Obj;
        SendtoTMW.getCurCase(caseObj.id);
        
        TMWSendOrderService obj = new TMWSendOrderService();
        TMWSendOrderService.SendToTmw(caseObj.id);
        
        Case cs =  [select id,SendtoTMW__c,SendtoTMWPerm__c,SFtoTMWSent__c,Origin,AccountId
                    FROM Case WHERE id = :caseObj.id];
        
        if(cs != null)
        {
            if (cs.AccountId == null)
            {
                cs.SendtoTMW__C = false;
                //result= 'Order cannot be submitted to TMW.  Please enter an Account on the Case.';
            }    
        }   // end if(cs != null)
    } //end static testMethod void getCurCase()
    
    @isTest
    static void getCurCase1(){
        Account ac=new Account(name='Test Account');
        Insert ac;
        List<Translation_Table__c> translation_table_Obj  =  [SELECT Integration_Status__c, Name from Translation_Table__c];
        System.assertEquals(true,translation_table_Obj.size()>0);
        
        
        Case caseObj = new Case(accountId = ac.id, status='Open', 
                                Fuel_Order_Status__c = 'Pending',  
                                Origin = 'Phone',
                                RQ_Delivery_Date__c=system.Today(), 
                                RQ_Delivery_Timeframe__c='9am-3pm',
                                Description='testing fuel order');
        // Add all required field here
        insert  caseObj;
        for(Translation_Table__c trans: translation_table_Obj){
            trans.IntegratedSite__c = ac.Id;
        }
        update  translation_table_Obj;
                SendtoTMW.getCurCase(caseObj.id);

        caseObj.SendtoTMW__c = true;
        update CaseObj;
        SendtoTMW.getCurCase(caseObj.id);                
    } //end static testMethod void getCurCase()       
}
 
Best Answer chosen by Eric Blaxton 11
Andrew GAndrew G
goodness.  without diving too deep, here's what i think.

1.  Use the code sample when posting code, it makes it easier to read.
User-added image

2.  Unsure why you are using the Test.startTest() & Test.stopTest() when doing your data setup. And then not using them when doing the actual test methods.  Have a read up on what those lines do in a test class.

3.  Unsure why you do data setup for an an account record and then insert another one for each Test method.

4.  Test methods should really do a single test or a limited number of tests.  Without reading all the logic, your method  static void getCurCase() seems quite long.

5. And lastly, that code is not covered because your test data only inserts the one Translation_Table__c  and that has a surrogate ID of  'TMW-Test123' 
Translation_Table__c translation_table_Obj = new Translation_Table__c(Integration_Status__c = false, IntegratedSite__c= ac.id, 
                                                                              Surrogate_ID__c='TMW-Test123');
and that value ('TMW-Test123') does not meet the IF ELSE statement check for 
if(null !=  listTranslationTable[0].Surrogate_ID__c){
                        String surrogateId = listTranslationTable[0].Surrogate_ID__c;
//to cover this IF statement you will need a Surrogate ID that does not start with TMW 
                        if(surrogateId.substring(0, 3) != 'TMW'){
///These 6 lines arent covered.
                            cs.SendtoTMW__C = false;
                            result= 'There is no valid TMW Surrogate ID for this case';
                            return result;
                        }
                    }else{
//to cover this ELSE statement you will need a Surrogate ID that is NULL
                        cs.SendtoTMW__C = false;
                        result= 'There is no valid TMW Surrogate ID for this case';
                        return result; 
                    }

Hope the above is helpful

Regards
Andrew