+ Start a Discussion
grandak koygrandak koy 

Create test class for query??

How do I write a test class for this query?

Part of my class:
public with sharing class AccountController {
    private Account account;
    private list<Requests__c> req;
    public id accountId {get;set;}
    public List<Account> act {get;set;}

    public AccountController(ApexPages.StandardController stdController) {
        this.account=(Account)stdController.getRecord();
    }
     public AccountController(){
    }

    public List<Requests__c> getOrder(){
        req = RequestQueries.RetrieveRequestsForAccount(account.id);
        return req;
    }
}

My test class so far:
  static testMethod void testMethodForAccount()
    {

         Account act = new Account();
         insert act;
         Request__c req = new Request__c();
         req.Account__c = act.id;
         insert req;

        Test.startTest();
   
  
 PageReference pageRef = Page.AccountsHomePage; 
        pageRef.getParameters().put('id', String.valueOf(act.id));
        Test.setCurrentPage(pageRef);

        ApexPages.currentPage().getParameters().put('id','act.id');

        ApexPages.StandardController sc = new ApexPages.standardController(acc1);

        AccountController a = new AccountController(sc);

         AccountController Anull = new AccountController(new ApexPages.StandardController(act));
       

   Test.stopTest();   

        }
       

    }
Best Answer chosen by grandak koy
Raj VakatiRaj Vakati
static testMethod void testMethodForAccount()
    {

         Account act = new Account();
         insert act;
         Request__c req = new Request__c();
         req.Account__c = act.id;
         insert req;

        Test.startTest();
   
  
 PageReference pageRef = Page.AccountsHomePage; 
        pageRef.getParameters().put('id', String.valueOf(act.id));
        Test.setCurrentPage(pageRef);

        ApexPages.currentPage().getParameters().put('id','act.id');

        ApexPages.StandardController sc = new ApexPages.standardController(acc1);

        AccountController accCon = new AccountController(sc);

		
		accCon.getOrder() ; 

       //To cover the default constructor 
	   AccountController accDefCOn = new AccountController()

   Test.stopTest();   

        }

 

All Answers

Raj VakatiRaj Vakati
static testMethod void testMethodForAccount()
    {

         Account act = new Account();
         insert act;
         Request__c req = new Request__c();
         req.Account__c = act.id;
         insert req;

        Test.startTest();
   
  
 PageReference pageRef = Page.AccountsHomePage; 
        pageRef.getParameters().put('id', String.valueOf(act.id));
        Test.setCurrentPage(pageRef);

        ApexPages.currentPage().getParameters().put('id','act.id');

        ApexPages.StandardController sc = new ApexPages.standardController(acc1);

        AccountController accCon = new AccountController(sc);

		
		accCon.getOrder() ; 

       //To cover the default constructor 
	   AccountController accDefCOn = new AccountController()

   Test.stopTest();   

        }

 
This was selected as the best answer
grandak koygrandak koy
Thank you, how would I cover this part:

 public PageReference SaveReq(){

       insert req;
       PageReference Page = new PageReference('/apex/AccountInformationWithRequests?id='+Req.Account__c);               
                Page.setRedirect(true);
                return Page;
Raj VakatiRaj Vakati
Use this line  after getOrder method in test class 

 accCon.SaveReq() ;