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 

I am new to Salesforce. Help needed on writing test method

Hi,

 

I am new to Salesforce. Help needed on writing test method. mu current method covers 44% please help to get it 75%.

 

	 	  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;
                }
                
                //string RowID;
                //public String getRowID() {return this.RowID;}
                //public List<TS_NASAAC_Item__c> TSIlst { get; set; }              
                //public PageReference DeleteRow()
                //{
                //string RowID = ApexPages.CurrentPage().getParameters().get('RowID');
                
                //TallySheetItemDelList= [select ID, 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 ID = :RowID limit 1];
      
               // if ( TallySheetItemDelList.size() > 0 )
                  //    {           
                  //      delete TallySheetItemDelList;   
                 //      }   
                       
                       
                // if for any reason we are missing the reference
                //if (RowID == null)
                //{
                  //return null;      }           
                  // find the account record within the collection      
                  //TS_NASAAC_Item__c tobeDeleted = null;      
                  //for(TS_NASAAC_Item__c a : TSIlst)       
                  //if (a.Id == RowID)
                  //{         
                   //tobeDeleted = a;         
                   // break;       
                    //}           
                     //if account record found delete it     
                     // if (tobeDeleted != null)
                     // {       Delete tobeDeleted;      }                  
                   //reset();           
                //delete TallySheetItemDelList;
               // 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());
 List<TS_NASAAC_Item__c> TallySheetItemList;
 List<TS_NASAAC_Item__c> TallySheetItemList1;
 

TallySheetItems controller = new TallySheetItems(sc);
controller.reset();

 TallySheetItemList = null;
controller.getTallySheetItems();

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 = 'a06T0000008OAyE' and IsDeleted__c = false order by CreatedDate limit 100 ];

controller.add();
controller.ExportExcel();
controller.getheaders();
controller.reset1();
controller.Completed();         
controller.saveItem();

}

/* Test Case ends....*/

 
}

 

 

krishnagkrishnag

check which lines are not getting covered and modify accordingly.

DevAccountDevAccount

Hi Krishnag,

 

sorry for the delayed reply..

 

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....*/


}