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
Rom_Rom_ 

Test Passed, but Code Coverage at 0%

Can anyone help with Code Coverage error. Tried to update a Class (that's already in production) to include one extra field (picklist - Status__c). Test passed, but getting 0% Code Coverage when deploying to production.

Class:
public class MultiAddUSUWHold {
    private ApexPages.StandardController std;
     
    // the associated USUWHs
   public List<US_UW_Hold__c> USUWH;
      
    // the chosen USUWH id - used when deleting a USUWH
    public Id chosenUSUWHId {get; set;}
     
    public MultiAddUSUWHold(ApexPages.StandardController stdCtrl)
    {
     std=stdCtrl;
    }
     
    public ISOOffice__Merchant_Application__c getApplication_Package()
    {
     return (ISOOffice__Merchant_Application__c) std.getRecord();
    }
 
    private boolean updateUSUWHs()
    {
        boolean result=true;
        if (null!=USUWH)
           {
           List<US_UW_Hold__c> updConts=new List<US_UW_Hold__c>();
              try
              {
               update USUWH;
              }
              catch (Exception e)
              {
                 String msg=e.getMessage();
                 integer pos;
                  
                 // if its field validation, this will be added to the messages by default
                 if (-1==(pos=msg.indexOf('FIELD_CUSTOM_VALIDATION_EXCEPTION, ')))
                 {
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, msg));
                 }
                  
                 result=false;
              }
           }
            
           return result;
    }
     
    public PageReference saveAndExit()
    {
     boolean result=true;
    result=updateUSUWHs();
      
     if (result)
     {
        // call standard controller save
        return std.save();
     }
     else
     {
      return null;
     }
    }
     
    public PageReference save()
    {
     Boolean result=true;
     PageReference pr=Page.USUWHolds;
     if (null!=getApplication_Package().id)
     {
      result=updateUSUWHs();
     }
     else
     {
      pr.setRedirect(true);
     }
      
     if (result)
     {
        // call standard controller save, but don't capture the return value which will redirect to view page
        std.save();
           ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Changes saved'));
     }
        pr.getParameters().put('id', getApplication_Package().id);
      
     return pr;
    }
 
    public void newUSUWH()
    {
       if (updateUSUWHs())
       {
          US_UW_Hold__c cont=new US_UW_Hold__c(Stip__c='', Application_Package__c=getApplication_Package().id);
          insert cont;
         
          // null the USUWHs list so that it is rebuilt
          USUWH=null;
       }
    }
     
    public void deleteUSUWH()
    {
       if (updateUSUWHs())
       {
          if (null!=chosenUSUWHId)
          {
             US_UW_Hold__c cont=new US_UW_Hold__c(Id=chosenUSUWHId);
              delete cont;
        
           // null the USUWHs list so that it is rebuilt
              USUWH=null;
              chosenUSUWHId=null;
          }
       }
    }
     
   public List<US_UW_Hold__c> getUSUWHs()
    {
       if ( (null!=getApplication_Package().id) && (USUWH == null) )
       {
           USUWH=[SELECT Id, Name, Stip__c, Received__c, Status__c, CreatedDate, Application_Package__r.Id
                         FROM US_UW_Hold__c 
                         WHERE Application_Package__r.Id = : getApplication_Package().ID
                         ORDER BY CreatedDate];
       }
                           
       return USUWH;
    }
}

Test:
@isTest(SeeAllData=true)
private class USUWHoldTest 
{
    public static testMethod void MultiAddUSUWHold()
    {
        ISOOffice__Merchant_Application__c App = new ISOOffice__Merchant_Application__c();
        App.Name =  'Test for US UW Holds';
        insert App;
        
        
        US_UW_Hold__c U = new US_UW_Hold__c();
        U.Stip__c = 'CRM Testing Stip';
        U.Received__c = true;
        U.Status__c = 'Received';
        U.Application_Package__c = App.Id;
        insert U;
                        
    }
}

Tried to take off SeeAllData on test class, same results.
Anyone have idea on how to resolve this? Any help will be much appreciated :)​
Ruwantha  LankathilakaRuwantha Lankathilaka
You haven't called single line of the class given(MultiAddUSUWHold) other thing is it is not recomended to use seeAllData

first create a test method as followos then confirm that it runs and give some perscentage of test  coverage 
 
static testMethod void testUpdateUSUWHs{
	
	
	ISOOffice__Merchant_Application__c App = new ISOOffice__Merchant_Application__c();
	App.Name =  'Test for US UW Holds';
	insert App;
	
	US_UW_Hold__c U = new US_UW_Hold__c();
	U.Stip__c = 'CRM Testing Stip';
	U.Received__c = true;
	U.Status__c = 'Received';
	U.Application_Package__c = App.Id;
	insert U;
	
	// call the calass 
	
	ApexPages.StandardController sc = new ApexPages.StandardController(App);
	MultiAddUSUWHold  mau = new MultiAddUSUWHold (sc);
	
	PageReference pageRef = Page.YOUR_PAGE_NAME;
	pageRef.getParameters().put('id', String.valueOf(App.Id));
	Test.setCurrentPage(pageRef);	
	
	mau.updateUSUWHs();	
}

 


Make sure to replace the YOUR_PAGE_NAME with your actual page name


If this helps you please mark it as the best answer so it could help the community in many ways.
Rom_Rom_
@Ruwanthalk: I'm getting "Unexpected token 'void'." when changing test class to code you provided.
Damon ShawDamon Shaw
Hi Rom_,

The error you received from Ruwanthalk's solution is likely becasue you copy/pasted it over your entire test class rather than adding it into the class.

Like Ruwanthalk pointed out, your current test doesn't actually call your class to be tested, all it does is insert 2 records.

try to avoid using seeAllData if you can, also when you call a method from your class use System.asserts to test that what you think is happening actually does, calling a save method might work but if the value you are trying to save gets changed to something else by a trigger or workflow you'll want to know about it.

here's a test for your save method, you will need similar test methods to cover your saveAndExit, newUSUWH, deleteUSUWH and getUSUWHs functions to make sure they all do what you expect.
 
@isTest
private class USUWHoldTest {
    
    @testSetup static void setupTestData() {

        ISOOffice__Merchant_Application__c App = new ISOOffice__Merchant_Application__c();
        App.Name =  'Test for US UW Holds';
        insert App;
        
        US_UW_Hold__c U = new US_UW_Hold__c();
        U.Stip__c = 'CRM Testing Stip';
        U.Received__c = true;
        U.Status__c = 'Received';
        U.Application_Package__c = App.Id;
        insert U;

    }

    @isTest static void saveTest() {
        ISOOffice__Merchant_Application__c app = [SELECT Id FROM ISOOffice__Merchant_Application__c WHERE Name = 'Test for US UW Holds'];
        US_UW_Hold__c usuwHold = [SELECT Id, Status__c FROM US_UW_Hold__c WHERE Stip__c = 'CRM Testing Stip'];

        ApexPages.StandardController sc = new ApexPages.StandardController(app);
        MultiAddUSUWHold  controllerEx = new MultiAddUSUWHold (sc);
        
        PageReference pageRef = Page.VISUALFORCE_PAGE;
        pageRef.getParameters().put('id', app.Id);
        Test.setCurrentPage(pageRef);   
        
        //check the usuwHold value is what we expect
        System.assertEquals('Received', usuwHold.Status__c);

        //set a new value and update
        mau.USUWH.Status__c = 'New';
        mau.save();

        //find the updated usuwHold
        US_UW_Hold__c updatedUSUWH = [SELECT Id, Status__c FROM US_UW_Hold__c WHERE Stip__c = 'CRM Testing Stip'];
        
        //check the new value has been set
        System.assertEquals('New', updatedUSUWH.Status__c);
    }
}

 
Ruwantha  LankathilakaRuwantha Lankathilaka

static testMethod void testUpdateUSUWHs(){
	
	
	ISOOffice__Merchant_Application__c App = new ISOOffice__Merchant_Application__c();
	App.Name =  'Test for US UW Holds';
	insert App;
	
	US_UW_Hold__c U = new US_UW_Hold__c();
	U.Stip__c = 'CRM Testing Stip';
	U.Received__c = true;
	U.Status__c = 'Received';
	U.Application_Package__c = App.Id;
	insert U;
	
	// call the calass 
	
	ApexPages.StandardController sc = new ApexPages.StandardController(App);
	MultiAddUSUWHold  mau = new MultiAddUSUWHold (sc);
	
	PageReference pageRef = Page.YOUR_PAGE_NAME;
	pageRef.getParameters().put('id', String.valueOf(App.Id));
	Test.setCurrentPage(pageRef);	
	
	mau.updateUSUWHs();	
}

There was a typo on the method. Please note I did not run the code.

Make sure to replace the YOUR_PAGE_NAME with your actual page name


If this helps you please mark it as the best answer so it could help the community in many ways.