Skip to main content
Hi all,

I need a test class for the below controller class.Please help

public without sharing class ClsAccountController{

    public List<Contact> Contactlist{get; set;}

    public Account acc{get;set;}

    public Id accId{get;set;}

        

    public ClsAccountController(){

        accId = ApexPages.currentPage().getParameters().get('id');

        Contactlist = [Select id,LastName from Contact where  

                          Account =: accId];

        

        

    }

    public ClsAccountController(ApexPages.StandardController controller){

        acc = (Account)controller.getRecord();

        Contactlist = [Select id,LastName from Contact where  

                          Account =: acc.Id];

        

       

    }

}

Thanks in advance,

Abhilash
2 answers
  1. Mar 17, 2018, 5:35 AM

    @isTest

    private class ClsAccountController_Test {

    private static testmethod void testStdCntrl(){

    Account testAccount = new Account(Name='Test Company Name123');

    insert testAccount;

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

    ClsAccountController accCon = new ClsAccountController(sc);

    PageReference pageRef = Page.demo;

    pageRef.getParameters().put('id', String.valueOf(testAccount.Id));

    Test.setCurrentPage(pageRef);

    }

    private static testmethod void testCntrl(){

    Account testAccount = new Account(Name='Test Company Name123');

    insert testAccount;

    ClsAccountController accCon = new ClsAccountController();

    PageReference pageRef = Page.demo;

    pageRef.getParameters().put('id', String.valueOf(testAccount.Id));

    Test.setCurrentPage(pageRef);

    }

    }

     
0/9000