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
Phuc Nguyen 18Phuc Nguyen 18 

Test class coverage help Visualforce page

Will someone help with a test class for this controller? 
public with sharing class Pdf_Permit_Controller 
{
    public Permit__c permit {get; set;}
    public ApexPages.StandardController controller {get;set;}
    public String permitId {get;set;}
	 public List<Permit_Fees__c> allfees;
    
    public Pdf_Permit_Controller(ApexPages.StandardController controller) 
    {
        permitId = ApexPages.currentPage().getParameters().get('id');// controller.getId();
		//System.debug(permitId);
		permit = (Permit__c)controller.getRecord();

    	permit = [Select ID,name, customer__c from Permit__c where id=:permitId LIMIT 1];

 		allfees = new  List<Permit_Fees__c>();
    	Permit_Fees__c feelist = new Permit_Fees__c();
 		feelist.Permit__c = permit.Permit_Fees__c;
        allfees.add(feelist);
}
}
thank you,
P
 
Best Answer chosen by Phuc Nguyen 18
Hemant_SoniHemant_Soni
Please share your test class. 

All Answers

Hemant_SoniHemant_Soni
Hi,
Please try below one.
@isTest
public class Pdf_Permit_Controller_Test{
	static testmethod void testPdfPermitController(){
		Permit__c oPermit = new Permit__c();
		oPermit.Name = 'Test';
		//you add all required fields here
		insert oPermit;
		
		Test.StartTest(); 
		ApexPages.StandardController sc = new ApexPages.StandardController(oPermit);
		Pdf_Permit_Controller oPdf_Permit_Controller = new Pdf_Permit_Controller(sc);

		PageReference pageRef = Page.Pdf_Permit; /*(Page Name)*/
		pageRef.getParameters().put('id', String.valueOf(oPermit.Id));
		Test.setCurrentPage(pageRef);

		Test.StopTest();
	}
}
Thanks
Hemant
Phuc Nguyen 18Phuc Nguyen 18
Hello Hermant_Soni,
I am getting an error while testing. 'System.QueryException: List has no rows for assignment to SObject'
Do I have to replicate this line of code: 'permit = [Select ID,name, customer__c from Permit__c where id=:permitId LIMIT 1];'

Cheers,
P
Hemant_SoniHemant_Soni
Hi,
Please try below code it will not give you any error but your code not looks good. Can you please explain what functionality you are trying to achive here.
public with sharing class Pdf_Permit_Controller 
{
    public Permit__c permit {get; set;}
    public ApexPages.StandardController controller {get;set;}
    public String permitId {get;set;}
	 public List<Permit_Fees__c> allfees;
    
    public Pdf_Permit_Controller(ApexPages.StandardController controller) 
    {
        permitId = ApexPages.currentPage().getParameters().get('id');// controller.getId();
		if(String.isNotBlank(permitId)){
			//System.debug(permitId);
			permit = (Permit__c)controller.getRecord();

			permit = [Select ID,name, customer__c from Permit__c where id=:permitId LIMIT 1];

			allfees = new  List<Permit_Fees__c>();
			Permit_Fees__c feelist = new Permit_Fees__c();
			feelist.Permit__c = permit.Permit_Fees__c;
			allfees.add(feelist);
		}
	}
}

Thanks
Hemant
Phuc Nguyen 18Phuc Nguyen 18
Hello Hemant,
Trying to populate data on a visulaforce page from the 'Permit object'(Main) and 'Permit_Fees__c'(child) object.  There are multiple Permit_Fees__c records.  
Thanks,
P
Hemant_SoniHemant_Soni
Hi,
I think whatever you are trying above is not fulfill your requirement. So you need to update your vf page.
Phuc Nguyen 18Phuc Nguyen 18
Hello Hemant,
The Controller is returning all of the fields and records for the visualforce page.  Are you saying the controller is incorrect?  If so please advise a better approach.
Thanks,
P
Phuc Nguyen 18Phuc Nguyen 18
Hemant,
I have cleaned up the code and ighlighted the lines that donot have any coverage:
public with sharing class Pdf_Permit_Controller 
{
    public Permit__c permit {get; set;}  --- no coverage
    public String permitId {get;set;}
    
    public Pdf_Permit_Controller(ApexPages.StandardController controller) 
    {
        permitId = ApexPages.currentPage().getParameters().get('id');// controller.getId();
		if(String.isNotBlank(permitId)){
			//System.debug(permitId);
			permit = [Select ID,name, customer__c from Permit__c where id=:permitId LIMIT 1];     --- no coverage
		}
	}
}

Any  suggestions?
Thanks,
P
Hemant_SoniHemant_Soni
Please share your test class. 
This was selected as the best answer
Phuc Nguyen 18Phuc Nguyen 18
Hemant,
I finally got it to work. Thanks for your help.
@isTest
public with sharing class Pdf_Permit_Controller_Test_Class {
    
    static testmethod void testPdfPermitController(){
        Permit__c permit = new Permit__c();
        permit.Name = 'Test Permit';
        permit.Market__c= 'Atlanta';
        permit.State__c= 'ALABAMA';

        insert permit;

        Permit_Fees__c oPermitFees = new Permit_Fees__c();
        oPermitFees.Permit__c= permit.id;
        oPermitFees.Fee_Type__c ='Permit Fee';
        oFees.Fee_Amount__c = 1.0;
        oPermitFees.Invoice__c = '123';

        insert oPermitFees;


        Test.StartTest(); 
            Apexpages.currentPage().getParameters().put('id', String.valueOf(permit.Id));
            ApexPages.StandardController sc = new 
            ApexPages.StandardController(permit);
            Pdf_Permit_Controller oPdf_Permit_Controller = new Pdf_Permit_Controller(sc);
            PageReference pageRef = Page.PermitPDF;
            Test.setCurrentPage(pageRef);
        Test.StopTest();

    }
}