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
Collen Mayer 6Collen Mayer 6 

Help with Test Class- VisualForce Controller

I'm very, very new to working with visual force and need to write a testclass for my controller below.  Could someone help get me started?  Many thanks in advance.

Best,
Collen
 
public class addMileageExpense {
public Expense_Report__c reports;
 public Mileage_expense__c del;
 public List<Mileage_expense__c> addexpenseList {get;set;}
 public List<Mileage_expense__c> delexpenseList {get;set;}
 public List<Mileage_expense__c> expenseList {get;set;}
 public Integer totalCount {get;set;}
 public Integer rowIndex {get;set;}
 public List<Mileage_expense__c> delexpense {get; set;} 
 public addMileageExpense(ApexPages.StandardController controller) {
 
 reports = (Expense_Report__c)controller.getRecord();
 expenseList = [Select id, Name, Expense_Report__c, Miles__c, Total_Miles__c,   
                Date__c, Prgm_1_Miles__c, Prgm_2_Miles__c, Prgm_3_Miles__c, Prgm_4_Miles__c, 
                Prgm_5_Miles__c, Prgm_6_Miles__c from Mileage_expense__c where Expense_Report__c =: reports.ID];
 totalCount = expenseList.size();
 
 delexpenseList = new List<Mileage_expense__c>();
 delexpense = new List<Mileage_expense__c>();
 }
 
 public void addRow(){
 addexpenseList = new List<Mileage_expense__c>();
 expenseList.add(new Mileage_expense__c(Expense_Report__c = reports.Id));
 }
 
 public PageReference ave(){
 
 upsert expenseList;
 delete delexpenseList;
 return (new ApexPages.StandardController(reports)).view();
 } 
 
 public void deleteRow(){
 
 rowIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex'));
 System.debug('row to be deleted ' + rowIndex );
 System.debug('row to be deleted '+ expenseList[rowIndex]);

     del = expenseList.remove(rowIndex);
     If(del.Id <> null) {delexpenseList.add(del);}
         
 
     }
 }
 
 
Best Answer chosen by Collen Mayer 6
Amit Chaudhary 8Amit Chaudhary 8
Try to add try catch in your test class and try below code
@isTest 
public class TestClassforMileage2 {
    public static testMethod void testmyController()
	{

	
        // Instantiate a new controller with all parameters in the page
        Expense_Report__c testExpenseReport = new Expense_Report__c();
			//testExpenseReport.User__c= '00536000002siWO'; 
			testExpenseReport.Start_Date__c= System.today();
			testExpenseReport.End_Date__c=System.today()+1;
			testExpenseReport.User__c= Userinfo.getUserId();
			
        insert testExpenseReport; 
	
	
		Mileage_expense__c milExp = new Mileage_expense__c();
			milExp.Expense_Report__c = testExpenseReport.id;
			milExp.Date__c = System.today()+1;
			milExp.Name = 'This is my test mileage expense.';
        insert milExp;

		Mileage_expense__c milExp1 = new Mileage_expense__c();
			milExp1.Expense_Report__c = testExpenseReport.id;
			milExp1.Date__c = System.today()+2;
			milExp1.Name = 'This is another test mileage expense.';
        insert milExp1;

        
		ApexPages.StandardController sc = new ApexPages.StandardController(testExpenseReport);
		addMileageExpense  controller = new addMileageExpense(sc);

		PageReference pageRef = Page.addMileageExpense2;
        Test.setCurrentPage(pageRef);
    	
		controller.addRow();
		ApexPages.currentPage().getParameters().put('rowIndex','0');
		controller.deleteRow();
		
		try
		{
			controller.ave();
		}
		Catch(Exception ee)
		{
			
		}
    }
}

Let us know if this will help you
 

All Answers

Collen Mayer 6Collen Mayer 6
Here's where I've gotten so far with my testclass.  I'm eager to learn so any help is appreciated. 
 
@isTest 
public class TestClassforMileage2 {
    public static testMethod void testmyController(){
        PageReference pageRef = Page.addMileageExpense2;
        Test.setCurrentPage(pageRef);
    	
        addMileageExpense controller = new addMileageExpense(new ApexPages.StandardController(new Expense_Report__c()));
        String nextPage = controller.ave().getUrl();
	
        // Instantiate a new controller with all parameters in the page
        Expense_Report__c testExpenseReport = new Expense_Report__c();
        testExpenseReport.User__c= '00536000002siWO'; 
        testExpenseReport.Start_Date__c= System.today();
        testExpenseReport.End_Date__c=System.today()+1;
        insert testExpenseReport; 
		controller.addRow();
        controller.addRow(); 
        controller.addRow(); 
        For (Mileage_expense__c expenses: controller.addexpenseList){
            expenses.Name = 'testName';
            expenses.Date__c= System.today();
            expenses.Expense_Report__c =testExpenseReport.Id; 
        }
        nextPage= controller.ave().getURL();
    }
}

 
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
@isTest 
public class TestClassforMileage2 {
    public static testMethod void testmyController()
	{

	
        // Instantiate a new controller with all parameters in the page
        Expense_Report__c testExpenseReport = new Expense_Report__c();
			//testExpenseReport.User__c= '00536000002siWO'; 
			testExpenseReport.Start_Date__c= System.today();
			testExpenseReport.End_Date__c=System.today()+1;
			// add all required fild
        insert testExpenseReport; 
	
	
		Mileage_expense__c milExp = new Mileage_expense__c();
			milExp.Expense_Report__c = testExpenseReport.id;
			// add all required fild
		insert milExp;

		Mileage_expense__c milExp1 = new Mileage_expense__c();
			milExp1.Expense_Report__c = testExpenseReport.id;
			// add all required fild
		insert milExp1;
		
	
		ApexPages.StandardController sc = new ApexPages.StandardController(testExpenseReport);
		addMileageExpense  controller = new addMileageExpense(sc);

		PageReference pageRef = Page.addMileageExpense2;
        Test.setCurrentPage(pageRef);
    	
		controller.addRow();
		ApexPages.currentPage().getParameters().put('rowIndex','0');
		controller.deleteRow();
		controller.ave();

		
		
    }
}

Let us know if this will help you
 
Collen Mayer 6Collen Mayer 6
Thanks so much.  I think we are very close.  I still get an error saying the upsert fails because a required field is missing in mileage expense.  This happens, I think, due to the controller.addRow() and controller.ave().  Since addRow creates a new record, I'm thinking I need to fill in the other required fields in the test method before deleting and saving.  If this sounds right, could you suggest the syntax to do this? 

Error: System.DmlException: Upsert failed. First exception on row 1; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Date__c]: [Date__c].  Stacktrace: Class.addMileageExpense.ave: line 29, column 1
Class.TestClassforMileage2.testmyController: line 39, column 1.

Here is your code with the other required fields filled in. 

Many thanks!
Collen
@isTest 
public class TestClassforMileage2 {
    public static testMethod void testmyController()
	{

	
        // Instantiate a new controller with all parameters in the page
        Expense_Report__c testExpenseReport = new Expense_Report__c();
			//testExpenseReport.User__c= '00536000002siWO'; 
			testExpenseReport.Start_Date__c= System.today();
			testExpenseReport.End_Date__c=System.today()+1;
			testExpenseReport.User__c= Userinfo.getUserId();
			
        insert testExpenseReport; 
	
	
		Mileage_expense__c milExp = new Mileage_expense__c();
			milExp.Expense_Report__c = testExpenseReport.id;
			milExp.Date__c = System.today()+1;
			milExp.Name = 'This is my test mileage expense.';
        insert milExp;

		Mileage_expense__c milExp1 = new Mileage_expense__c();
			milExp1.Expense_Report__c = testExpenseReport.id;
			milExp1.Date__c = System.today()+2;
			milExp1.Name = 'This is another test mileage expense.';
        insert milExp1;

        
		ApexPages.StandardController sc = new ApexPages.StandardController(testExpenseReport);
		addMileageExpense  controller = new addMileageExpense(sc);

		PageReference pageRef = Page.addMileageExpense2;
        Test.setCurrentPage(pageRef);
    	
		controller.addRow();
		ApexPages.currentPage().getParameters().put('rowIndex','0');
		controller.deleteRow();
		controller.ave();

        
        
		
		
    }
}

 
Amit Chaudhary 8Amit Chaudhary 8
It look like like Date__c Field is missing in your code. Update your code like below
public class addMileageExpense {
public Expense_Report__c reports;
 public Mileage_expense__c del;
 public List<Mileage_expense__c> addexpenseList {get;set;}
 public List<Mileage_expense__c> delexpenseList {get;set;}
 public List<Mileage_expense__c> expenseList {get;set;}
 public Integer totalCount {get;set;}
 public Integer rowIndex {get;set;}
 public List<Mileage_expense__c> delexpense {get; set;} 
 public addMileageExpense(ApexPages.StandardController controller) {
 
 reports = (Expense_Report__c)controller.getRecord();
 expenseList = [Select id, Name, Expense_Report__c, Miles__c, Total_Miles__c,   
                Date__c, Prgm_1_Miles__c, Prgm_2_Miles__c, Prgm_3_Miles__c, Prgm_4_Miles__c, 
                Prgm_5_Miles__c, Prgm_6_Miles__c from Mileage_expense__c where Expense_Report__c =: reports.ID];
 totalCount = expenseList.size();
 
 delexpenseList = new List<Mileage_expense__c>();
 delexpense = new List<Mileage_expense__c>();
 }
 
 public void addRow(){
 addexpenseList = new List<Mileage_expense__c>();
 expenseList.add(new Mileage_expense__c(Expense_Report__c = reports.Id , Date__c = System.today()+2 ));
 }
 
 public PageReference ave(){
 
 upsert expenseList;
 delete delexpenseList;
 return (new ApexPages.StandardController(reports)).view();
 } 
 
 public void deleteRow(){
 
 rowIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex'));
 System.debug('row to be deleted ' + rowIndex );
 System.debug('row to be deleted '+ expenseList[rowIndex]);

     del = expenseList.remove(rowIndex);
     If(del.Id <> null) {delexpenseList.add(del);}
         
 
     }
 }





Please try below test class
@isTest 
public class TestClassforMileage2 {
    public static testMethod void testmyController()
	{

	
        // Instantiate a new controller with all parameters in the page
        Expense_Report__c testExpenseReport = new Expense_Report__c();
			//testExpenseReport.User__c= '00536000002siWO'; 
			testExpenseReport.Start_Date__c= System.today();
			testExpenseReport.End_Date__c=System.today()+1;
			testExpenseReport.User__c= Userinfo.getUserId();
			
        insert testExpenseReport; 
	
	
		Mileage_expense__c milExp = new Mileage_expense__c();
			milExp.Expense_Report__c = testExpenseReport.id;
			milExp.Date__c = System.today()+1;
			milExp.Name = 'This is my test mileage expense.';
        insert milExp;

		Mileage_expense__c milExp1 = new Mileage_expense__c();
			milExp1.Expense_Report__c = testExpenseReport.id;
			milExp1.Date__c = System.today()+2;
			milExp1.Name = 'This is another test mileage expense.';
        insert milExp1;

        
		ApexPages.StandardController sc = new ApexPages.StandardController(testExpenseReport);
		addMileageExpense  controller = new addMileageExpense(sc);

		PageReference pageRef = Page.addMileageExpense2;
        Test.setCurrentPage(pageRef);
    	
		controller.addRow();
		ApexPages.currentPage().getParameters().put('rowIndex','0');
		controller.deleteRow();
		
		try
		{
			controller.ave();
		}
		Catch(Exception ee)
		{
			
		}
    }
}

Let us know if this will help you
 
Collen Mayer 6Collen Mayer 6
Thanks.  So the date field in my code is something the user enters on the visual force page.  In my test class, I've just been using "System.today()" to get a sample date.  The user adds row using the "add row button" and then has to enter in the required fields, which includes the date field and the name field.  Then the user is able to "save".  I think what I need to do is fill in this data in my test class to simulate user input, correct?  Could you suggest a syntax to do that?
 
Amit Chaudhary 8Amit Chaudhary 8
Try to add try catch in your test class and try below code
@isTest 
public class TestClassforMileage2 {
    public static testMethod void testmyController()
	{

	
        // Instantiate a new controller with all parameters in the page
        Expense_Report__c testExpenseReport = new Expense_Report__c();
			//testExpenseReport.User__c= '00536000002siWO'; 
			testExpenseReport.Start_Date__c= System.today();
			testExpenseReport.End_Date__c=System.today()+1;
			testExpenseReport.User__c= Userinfo.getUserId();
			
        insert testExpenseReport; 
	
	
		Mileage_expense__c milExp = new Mileage_expense__c();
			milExp.Expense_Report__c = testExpenseReport.id;
			milExp.Date__c = System.today()+1;
			milExp.Name = 'This is my test mileage expense.';
        insert milExp;

		Mileage_expense__c milExp1 = new Mileage_expense__c();
			milExp1.Expense_Report__c = testExpenseReport.id;
			milExp1.Date__c = System.today()+2;
			milExp1.Name = 'This is another test mileage expense.';
        insert milExp1;

        
		ApexPages.StandardController sc = new ApexPages.StandardController(testExpenseReport);
		addMileageExpense  controller = new addMileageExpense(sc);

		PageReference pageRef = Page.addMileageExpense2;
        Test.setCurrentPage(pageRef);
    	
		controller.addRow();
		ApexPages.currentPage().getParameters().put('rowIndex','0');
		controller.deleteRow();
		
		try
		{
			controller.ave();
		}
		Catch(Exception ee)
		{
			
		}
    }
}

Let us know if this will help you
 
This was selected as the best answer
Collen Mayer 6Collen Mayer 6
Great.  So with the try, catch it passed with 91% coverage.  It throws (and catches) an exception at the
 
upsert expenseList;

Since it's missing the required field from the user and it never gets to this line and these show as non-covered:
 
delete delexpenseList;
 return (new ApexPages.StandardController(reports)).view();


But I assume this is "good enough" since we only need 75% correct?

Thanks,

Collen

Collen Mayer 6Collen Mayer 6
Most grateful!  Really appreciate your help.