+ Start a Discussion
DevAccountDevAccount 

How to write Test method for Upsert Statement. Please help..

How to write Test method for Upsert Statement. Please help..

 

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, IsDeleted__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 PageReference saveItem()
{
upsert TallySheetItemList;
reset();
return null;
}

Currently I have it in my testMethod

 

/* 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.reset();
controller.saveItem();
}

/* Test Case ends....*/

 

AvromAvrom

I'm getting 100% test coverage when I run this (with some additional stuff, like a constructor).

 

What's the behavior that you're expecting but not seeing?

 

[Edit]: One possibility that occurs to me is that you're expecting to see the results of the upsert. But you won't--transactions inside a testMethod are rolled back when that method ends, so unless you check the results *during method execution*, you won't see any changes.

 

DevAccountDevAccount

Hi Avrom,

 

Even though my current test method covers 77% (red fonted are not currently covered), but salesforce is not allowing me to move it production 

 

Below is my whole apex class with test method and every thing. I would certainly appreciate any kind of help on corrrecting my test method to cover 100% and move it to production.

 

 

 

 

public with sharing class TallySheetItems {

private ApexPages.StandardController controller {get; set;}

//added an instance varaible for the standard controller
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;
List<TS_NASAAC_Item__c> TallySheetItemList1;
public double total { get; set; }
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, IsDeleted__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 void setTallySheetItems(List<TS_NASAAC_Item__c> TallySheetItems) {
TallySheetItemList = TallySheetItems;
}

public PageReference saveItem() {
total = 0;
for(TS_NASAAC_Item__c o: TallySheetItemList)
{
if(o.GR_WT_KGS__c!= null)
{
total += o.GR_WT_KGS__c;
}
}
TS.TOTAL__c = total;
update TS;
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;
}

public PageReference ExportExcel()
{
TS.Exported__c = true;
update TS;
PageReference customPage = Page.vfTallySheetExport;
customPage.setRedirect(true);
customPage.getParameters().put('id', recordId);
return customPage;

}

public string[] getheaders()
{
return new string [] {'Melt#', 'BND#', 'GR WT KGS', 'TARE', 'NET WT KGS', 'Melt Date', 'INGOTS', 'Ref#'} ;
}

public List<TS_NASAAC_Item__c> getTallySheetItems1() {
if(TallySheetItemList== null) reset1();
return TallySheetItemList;
}

public PageReference reset1() {
TallySheetItemList= [select MELT__c, BND__c, GR_WT_KGS__c, Tare__c,
NET_WT_KGS__c, Melt_Date__c, INGOTS__c, Ref__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 PageReference Completed() {
TS.Completed__c = true;
update TS;
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();
}

static testMethod void myUnitTest1() {

PageReference PageRef = Page.vfTallySheet;
test.setCurrentPage(PageRef);
Apexpages.Standardcontroller sc = new Apexpages.Standardcontroller(new Tally_Sheet__c());
TallySheetItems controller = new TallySheetItems(sc);
double total;
total = 10;
List<TS_NASAAC_Item__c> TallySheetItemList;
controller.setTallySheetItems(TallySheetItemList);
controller.getTallySheetItems1();
controller.getheaders();
controller.reset1();
controller.ExportExcel();
controller.Completed();
}

/* Test Case ends....*/


}

 

 

When I moved this to production I got the below two failure..

 

TallySheetItems.myUnitTest()Class50
Failure Message: "System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []", Failure Stack Trace: "Class.TallySheetItems.saveItem: line 50, column 22 Class.TallySheetItems.myUnitTest: line 108, column 1 External entry point"
TallySheetItems.myUnitTest1()Class66
Failure Message: "System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []", Failure Stack Trace: "Class.TallySheetItems.ExportExcel: line 66, column 18 Class.TallySheetItems.myUnitTest1: line 124, column 1 External entry point"

 

 

TallySheetItems.myUnitTest()Class50 Failure Message: "System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []", Failure Stack Trace: "Class.TallySheetItems.saveItem: line 50, column 22 Class.TallySheetItems.myUnitTest: line 108, column 1 External entry point"
TallySheetItems.myUnitTest1()Class66 Failure Message: "System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []", Failure Stack Trace: "Class.TallySheetItems.ExportExcel: line 66, column 18 Class.TallySheetItems.myUnitTest1: line 124, column 1 External entry point"
     
     

 

AvromAvrom

Aha. I think I see what's wrong.

 

You're calling

 

 

update TS;

 

 

in the method saveItem(). But TS, in your test method, is a brand new TallySheet, so can't be updated. You probably mean

 

 

upsert TS;

 

By the way, I'm not quite sure why you're creating the PageReference in your testMethods. You don't use it to get your controller (instead, you're calling the controller's constructor directly), and you don't really do anything else with it as far as I can see.