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
DevAccountDevAccount 

Help needed on writing test method.

Hi,

 

Help needed on writing test method.

 

I checked the line coverage and found the below red fonted were not covered, Please help to write test cases for the Upsert statement.

 

 

public with sharing class TallySheetItems {

private ApexPages.StandardController controller {get; set;}

Tally_Sheet__c TS;
String recordId;

public TallySheetItems(ApexPages.StandardController stdController)
{
recordId = stdController.getId();
TS = (Tally_Sheet__c) stdController.getRecord();
this.controller=stdController;
}

List<TS_NASAAC_Item__c> TallySheetItemList;

public PageReference reset()
{
TallySheetItemList= [select ID, MELT__c, BND__c, GR_WT_KGS__c, Tare__c,
NET_WT_KGS__c, Melt_Date__c, INGOTS__c, Ref__c, IsDelete d__c from TS_NASAAC_Item__c where Tally_Sheet_Ref__c = :TS.ID and IsDeleted__c = false order by CreatedDate limit 100 ];
return null;
}

public List<TS_NASAAC_Item__c> getTallySheetItems()
{
if(TallySheetItemList== null) reset();
return TallySheetItemList;
}

public PageReference saveItem()
{
upsert TallySheetItemList;
reset();
return null;
}

public PageReference add()
{
TS_NASAAC_Item__c TSI = new TS_NASAAC_Item__c();
TSI.Tally_Sheet_Ref__c = TS.ID;
TallySheetItemList.add(TSI);
return null;
}


/* Test Case */

static testMethod void myUnitTest() {
PageReference PageRef = Page.vfTallySheet;
test.setCurrentPage(PageRef);
Apexpages.Standardcontroller sc = new Apexpages.Standardcontroller(new Tally_Sheet__c());
TallySheetItems controller = new TallySheetItems(sc);
controller.getTallySheetItems();
controller.reset();
controller.add();
controller.saveItem();
}

/* Test Case ends....*/


}



Any help would certainly appreciated.

Thanks
Dev

 

 

gv007gv007

insert some dummy data in the folloeing table TS_NASAAC_Item__c and query it in yours test method it will improve.

 

DevAccountDevAccount

As per your suggestion, I added the dummy data into my testMethod as below green fonted but it still not covering the upsert.. Let me know, where am I going wrong

 

 

static testMethod void myUnitTest() {
List<TS_NASAAC_Item__c> TSIList = new List<TS_NASAAC_Item__c>{};
TS_NASAAC_Item__c TSI = New TS_NASAAC_Item__c(MELT__c = 'Test Account', GR_WT_KGS__c = 2);
TSIList.add(TSI);

PageReference PageRef = Page.vfTallySheet;
test.setCurrentPage(PageRef);
Apexpages.Standardcontroller sc = new Apexpages.Standardcontroller(new Tally_Sheet__c());
TallySheetItems controller = new TallySheetItems(sc);
controller.getTallySheetItems();
controller.reset();
controller.add();
controller.saveItem();
}

 

 

bmabma

Just curious, is the method "saveItem()" get executed at all? There maybe exception in "add()" that prevent "saveItem()" from executing.

 

Probably adding "system.debug" statement or "system.assert" calls in your method would help narrow down the issue.