You need to sign in to do that
Don't have an account?
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:
Test:
Tried to take off SeeAllData on test class, same results.
Anyone have idea on how to resolve this? Any help will be much appreciated :)
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 :)
first create a test method as followos then confirm that it runs and give some perscentage of test coverage
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.
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.
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.