Skip to main content
Hi all,

 

How to write the test class for VF Controller.

 

public With Sharing class Invoice {

 

    public Invoice_Master__c invo{get;set;}

 

    List<Invoice_Master__c> listIn {get;set;}

 

    public Invoice(ApexPages.StandardController Controller){

 

        listIn=[Select id,Name,(Select id,Name From Invoice_Line_Items__r) From Invoice_Master__c];

 

    }

 

}
15 answers
  1. Jun 3, 2020, 8:33 AM
    Awesome.

     

    Thanks for sharing

     

    Now try with adding one line:

     

    Invoice invCtrl = new Invoice(sc);

     

     

    @istest

    public class Invoicetest {

    static testMethod void testMethod1(){

    Account acc = new Account();

    acc.Name = 'Test';

    insert acc;

    Invoice_Master__c Inv = new Invoice_Master__c();

    Inv.Account__c = acc.Id;

    insert Inv;

    product2 pro = new Product2();

    pro.Name = 'Test';

    insert pro;

    Invoice_Line_Item__c Invoice = new Invoice_Line_Item__c();

    Invoice.Invoice__c = Inv.Id;

    Invoice.Product__c = Pro.Id;

    Invoice.Quantity__c = 10;

    Invoice.UnitPrice__c = 1000.00;

    Invoice.Months__c = 2.00;

    insert Invoice;

    Test.startTest();

    ApexPages.StandardController sc = new ApexPages.StandardController(Inv);

    ApexPages.currentPage().getParameters().put('id', String.valueOf(Invoice.Id));

    Invoice invCtrl = new Invoice(sc);

    Test.stopTest();

    }

    }

     

     
0/9000