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
Damon HedgspethDamon Hedgspeth 

Need quick help with Apex testing

So, I've managed to learn a bit of apex. Now I really need a hand with testing the code so that I can move it to production.  Could someone please help me with the following?
public class multiAddCEx {

public Id cID = ApexPages.currentPage().getParameters().get('Id'); //grab the Vendor Bill ID
//Integer count = 0;
string num_name = '';
// Lookup against Available Cost Codes Junction Object
public List<SelectOption> costcodes {get; set;}
    public multiAddCEx ()
    {
        costcodes  = new List<SelectOption>() ;
          for(Cost_Code_Connection__c cc : [select Cost_Code_Junction2__r.Cost_Code_ID__c , Cost_Code_Percentage__c, id,name from Cost_Code_Connection__c where Cost_Code_Connection__c.Bill_Item_Junction__c =: cID order by Cost_Code_Junction2__r.Cost_Code_ID__c, Cost_Code_Percentage__c ])
        {
        costcodes.add(new SelectOption(cc.Cost_Code_Junction2__r.Cost_Code_ID__c +' - '+ cc.Cost_Code_Percentage__c +'%',cc.Cost_Code_Junction2__r.Cost_Code_ID__c +' - '+ cc.Cost_Code_Percentage__c +'%' )) ;
        }
  }
//Lookup to find User Intranet ID in User records
public List<SelectOption> intranetids {get; set;}
    {
        intranetids = new List<SelectOption>() ;
        intranetids.add(new SelectOption('-Select-','-Select-'));
        for(User ui : [select Intranet_ID__c , id, name, IsActive from User where IsActive = True order by Intranet_ID__c])
                {
if (ui.Intranet_ID__c != null){ 
num_name = ui.name;
        intranetids.add(new SelectOption(ui.Intranet_ID__c, ui.Intranet_ID__c +' - '+ num_name  )) ;
}
        }    
    }
List <Bill_Allotment__c> CExList;
public Id getID {get; set;}
public PageReference reset()  { //pull at most 8 expense records to show so we don't clutter up the page
CExList = [select name, Bill_Allotment__c.Phone_Number__c ,  Allotment_Amount__c, Bill_Allotment__c.Allotment_User_Id__c,  Bill_Allotment__c.Bill_Cost_Code__c  from Bill_Allotment__c where Bill_Item__c =: cID order by createddate limit 0 ];
return null; }
public List <Bill_Allotment__c> getCExs() {
 if(CExList == null) reset();
 return CExList;
  }
public void setAccounts(List <Bill_Allotment__c> cexs) {
   CExList = cexs;}
public PageReference save() {//upsert records on save
try{
       upsert CExList;
       ApexPages.Message myMsg = new ApexPages.message(ApexPages.Severity.Info, 'Records Saved Successfully'); //show confirmation message on save
ApexPages.addMessage(myMsg);
return null;   
       }
    catch(DmlException ex){
   
        ApexPages.addMessages(ex);
       }
    return null;       
}
//ApexPages.Message myMsg = new ApexPages.message(ApexPages.Severity.Info, 'Records Saved Successfully'); //show confirmation message on save
//ApexPages.addMessage(myMsg);
//return null;}
public PageReference cancel() {//close window without upsert
return null;
}
public PageReference add() {
//count++;
CExList.add(New Bill_Allotment__c(Bill_Item__c = cID)); //add records to Bill Items and associate with current Bill
return null; }
 //public Integer getCount() {
        //return count;
   // }
}
Best Answer chosen by Damon Hedgspeth
Chidambar ReddyChidambar Reddy
Hi Damon,
 
@isTest 
private class multiAddCEx_Tests{
	
	@isTest static void testmultiAddCEx(){
		//set your page 
		test.setCurrentPageReference(page.YOURPAGENAME);

		//as it is extension
		// create the record from where you access this page
		// I presume it is vendor Bill, so insert a record below
		Vendor_Bill__c vb = new Vendor_Bill__c();
		//fill mandatory fields
		insert vb;

		//give the id to the class
		ApexPages.CurrentPage.getParameters().put('id',vb.Id);

		// insert Cost_Code_Connection__c  and its parent records, insert bill amounts

		// insert user record to match the query (you can query profile and assign it to the new user)

		multiAddCEx ext = new multiAddCEx();
		List<SelectOption> temp = ext.intranetids;
		test.startTest();
		ext.add();
		ext.save();
		ext.cancel();
		test.stopTest();

	}

}

Thanks
Choose it as best answer if it has helped you

All Answers

Chidambar ReddyChidambar Reddy
Hi Damon,
 
@isTest 
private class multiAddCEx_Tests{
	
	@isTest static void testmultiAddCEx(){
		//set your page 
		test.setCurrentPageReference(page.YOURPAGENAME);

		//as it is extension
		// create the record from where you access this page
		// I presume it is vendor Bill, so insert a record below
		Vendor_Bill__c vb = new Vendor_Bill__c();
		//fill mandatory fields
		insert vb;

		//give the id to the class
		ApexPages.CurrentPage.getParameters().put('id',vb.Id);

		// insert Cost_Code_Connection__c  and its parent records, insert bill amounts

		// insert user record to match the query (you can query profile and assign it to the new user)

		multiAddCEx ext = new multiAddCEx();
		List<SelectOption> temp = ext.intranetids;
		test.startTest();
		ext.add();
		ext.save();
		ext.cancel();
		test.stopTest();

	}

}

Thanks
Choose it as best answer if it has helped you
This was selected as the best answer
Damon HedgspethDamon Hedgspeth
Chidambar - 

Thank you.  I spent another 30 minutes on the code that you provided and made it work for me. It may not be perfect but it is my first test code and IT WORKS!
Below is the final:
@isTest  private class multiAddCEx_Tests{  
@isTest static void testmultiAddCEx(){ 
//set your page  
test.setCurrentPageReference(page.BillLI);  
//as it is extension 
// create the record from where you access this page 
// I presume it is vendor Bill, so insert a record below 

Account acc = new Account();
acc.name='ATT-Test';
insert acc;
ApexPages.currentPage().getParameters().put('id',acc.Id);  


Vendor_Bill__c vb = new Vendor_Bill__c(); 
//fill mandatory fields 
vb.name='TestWireless';
vb.Vendor__c= acc.Id;
vb.Bill_Account_Number__c='29299';
insert vb; 
//give the id to the class 
ApexPages.currentPage().getParameters().put('id',vb.Id);  
// insert Cost_Code_Connection__c and its parent records, insert bill amounts  
// insert user record to match the query (you can query profile and assign it to the new user)  

Bill_Item__c bi = new Bill_Item__c();
bi.Month_Processed__c=12;
bi.Vendor_Account__c=vb.Id;
bi.Invoice_Number__c='237723';
insert bi;
ApexPages.currentPage().getParameters().put('id',bi.Id);  

Bill_Allotment__c ext = new Bill_Allotment__c(); 
ext.Phone_Number__c='222-333-3333';
ext.Allotment_Amount__c=2;
ext.Bill_Item__c=bi.Id;
//ext.Allotment_User_Id__c='1001';
ext.Bill_Cost_Code__c='[0690.99 T - 100%]';
insert ext;
ApexPages.currentPage().getParameters().put('id',ext.Id);
  
multiAddCEx ext1 = new multiAddCEx(); 
//List<SelectOption> temp = ext1.intranetids;
//List<SelectOption> temp1 = ext1.costcodes; 


test.startTest(); 
ext1.reset();
ext1.add(); 
ext1.save(); 
ext1.cancel(); 
test.stopTest(); 
 }
   }

 
Chidambar ReddyChidambar Reddy
Hi, you only need to set the Id once (based on the standard controller)
 
@isTest  private class multiAddCEx_Tests{  
	@isTest static void testmultiAddCEx(){ 
		//set your page  
		test.setCurrentPageReference(page.BillLI);  
		//as it is extension 
		// create the record from where you access this page 
		// I presume it is vendor Bill, so insert a record below 

		Account acc = new Account();
		acc.name='ATT-Test';
		insert acc;
		//ApexPages.currentPage().getParameters().put('id',acc.Id);  
		// NO NEED OF ABOVE LINE

		Vendor_Bill__c vb = new Vendor_Bill__c(); 
		//fill mandatory fields 
		vb.name='TestWireless';
		vb.Vendor__c= acc.Id;
		vb.Bill_Account_Number__c='29299';
		insert vb; 
		//give the id to the class 
		ApexPages.currentPage().getParameters().put('id',vb.Id);  
		// insert Cost_Code_Connection__c and its parent records, insert bill amounts  
		// insert user record to match the query (you can query profile and assign it to the new user)  

		Bill_Item__c bi = new Bill_Item__c();
		bi.Month_Processed__c=12;
		bi.Vendor_Account__c=vb.Id;
		bi.Invoice_Number__c='237723';
		insert bi;
		//ApexPages.currentPage().getParameters().put('id',bi.Id);  
		// NO NEED OF ABOVE LINE
		Bill_Allotment__c ext = new Bill_Allotment__c(); 
		ext.Phone_Number__c='222-333-3333';
		ext.Allotment_Amount__c=2;
		ext.Bill_Item__c=bi.Id;
		//ext.Allotment_User_Id__c='1001';
		ext.Bill_Cost_Code__c='[0690.99 T - 100%]';
		insert ext;
		//ApexPages.currentPage().getParameters().put('id',ext.Id);
	 	// NO NEED OF ABOVE LINE 
		multiAddCEx ext1 = new multiAddCEx(); 
		//List<SelectOption> temp = ext1.intranetids;
		//List<SelectOption> temp1 = ext1.costcodes; 


		test.startTest(); 
			ext1.reset();
			ext1.add(); 
			ext1.save(); 
			ext1.cancel(); 
		test.stopTest(); 
 	}
}