-1

enter image description here

enter image description here

How can I test below code which is inside controller?

public with sharing class AccountDetails{
    public Id accRecId;
    public  List<account> accRec { get; set; }  

  public ApexPages.StandardController controller;
    public AccountDetails(ApexPages.StandardController controller){
    this.controller=controller;
        accRecId =ApexPages.currentPage().getParameters().get('id');
        if(accRecId != null){            
            accRec =  [select id,name,Rating,AccountNumber,Phone,Type,Industry,(select id,FirstName,LastName,Phone from contacts),
                       (select id, name,CloseDate,StageName from Opportunities ) from account where id=:accRecId];            
        }
    }  
}

@isTest
public class TestAccDetails {
    @isTest
  public static void testSesion()
    {       
        Account a = new Account();
        a.Name='Test Account' ;
        insert a;      
         Test.StartTest();
        ApexPages.StandardController ctrl = new ApexPages.StandardController(a);
        AccountDetails ext = new AccountDetails(ctrl);
        PageReference myVfPage = Page.AccountDetailRelatedPage;
        myVfPage.getParameters().put('accId', String.valueOf(a.Id));
        Test.setCurrentPage(myVfPage);
        ApexPages.currentPage().getParameters().put('id',a.Id);
          Test.StopTest();



       // List<Account> accRecList = [select id,name,Rating,AccountNumber,Phone,Type,Industry,(select id,FirstName,LastName,Phone from contacts),
       //                             (select id, name,CloseDate,StageName from Opportunities ) from account where id=:a.Id];        
       // System.assertEquals('Test Account', accRecList[0].Name); 
        //System.assertEquals('Hot', accRecList[0].Rating); 

    }
}
  • 1
    Its better to copy and paste your code. – Reshma Jan 18 at 11:22
0

Your test class need some tweaks.

1) You should use parameter Id instead of accId

2) Test.setCurrentPage should be set before calling controller methods.

Try with below test class and check

@isTest
public class TestAccDetails {
    @isTest
    public static void testSesion()
    {       
        Account a = new Account();
        a.Name='Test Account' ;
        insert a;      

        Test.StartTest();

        PageReference myVfPage = Page.AccountDetailRelatedPage;
        Test.setCurrentPage(myVfPage);
        ApexPages.currentPage().getParameters().put('id',a.Id);

        ApexPages.StandardController ctrl = new ApexPages.StandardController(a);
        AccountDetails ext = new AccountDetails(ctrl);

        Test.StopTest();

        //...Assertions here...
    }
}

Also I recommend to update your controller code to

accRecId = controller.getId();

Instead of fetching from url parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.