Skip to main content
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 :)​
4 Antworten
  1. 29. Sept. 2017, 02:34
    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);

    }

    }

     
0/9000