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
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12 

Test class Code coverage

How can we cover the code for the below controller's code?


if(Apexpages.currentPage().getParameters().get('Export') != null){
                isExport = true;
            }
            else{
                isExport = false;
            }

Thank you.
Best Answer chosen by rmranjith8881.3927046400771116E12
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12
Thanks pradeep,
If my visulaforce url is like this:
     https://c.cs6.visual.force.com/apex/StudentSponsorPage?sfdc.tabName=01rN00000008gWC 

In this what is my pagereference url &
then

what is my testTitleName?

Thanks in advance

All Answers

rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12
Iam covering the code by callingg assert() as below,
but one error occurs as: expecting a semi-colon, found '(' at public boolean isExport(){ line

System.Assert(isExport());
        public boolean isExport(){
           return (isExport()!=NULL ? true : false);
        }
        stdntSC.export();

For this what is the solution?

Thanks in advance

Pradeep RajuPradeep Raju
Hi Ranjith,

It should be covered in following way from Test Class

PageReference dummypgref = new PageReference('http://www.google.com');
Test.setCurrentPage(dummypgref );
ApexPages.CurrentPage().getParameters().put('Export','testTitleName');
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12
Thanks prdeep,
so,instead of usin assert() we can use pagereference right?

can you expalin me the meaning of this 'http://www.google.com' url.
and can you write the correct code for me..

Thanks  in advance pradeepraju1902.
Pradeep RajuPradeep Raju
I provided google.com as example.You should be using your page that is associated to your controller in the parameters.

& then

ApexPages.CurrentPage().getParameters().put('Export','testTitleName'); where titleName might be Id of a record or some value 
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12
Thanks pradeep,
If my visulaforce url is like this:
     https://c.cs6.visual.force.com/apex/StudentSponsorPage?sfdc.tabName=01rN00000008gWC 

In this what is my pagereference url &
then

what is my testTitleName?

Thanks in advance
This was selected as the best answer
Pradeep RajuPradeep Raju
PageReference pg = page.StudentSponsorPage;
Test.setCurrentPage(pg);

ApexPages.CurrentPage().getParameters().put('Export',test.id);

In Page what your are trying to insert ? I mean test.id should be the record which your are trying to insert from your page 

then Controller c=new Controller();
Pradeep RajuPradeep Raju
Welcome then that part of code is covered ?
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12
No not yet,
Iam updated my code as below

PageReference pg = page.StudentSponsorPage;
        Test.setCurrentPage(pg);

        ApexPages.CurrentPage().getParameters().put('Export','sfdc.tabName=01rN00000008gWC');

controller.export();

still not covered the code..
there is no errors while saving,it is running successfully,but the code is not covered so far.
what  is the problem?

Thanks in avance.
Pradeep RajuPradeep Raju
Try this ApexPages.CurrentPage().getParameters().put('Export',sfdc.tabName);
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12
same situation not covered
Pradeep RajuPradeep Raju
can u post your class code here ?
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12
public with sharing class studentSponsorController{

    public List<Opportunity> oppList{get; set;}//List of all Opportunities for data table
    public Opportunity oppObj {get; set;}//object for oppotunity to get content on visualforce page
   
    private String sortDirection = 'ASC';//variables to sort data table columns
    private String sortExp = 'name';//variables to sort data table columns
   
    public Contact con {set; get;}//variable for contact fo get content on visual force page
   
    public boolean isExport {get;set;}//variable to export data into excel
   
    //logic to sort the data table values
    public String sortExpression{
        get{
            return sortExp;
        }
        set{
            //if the column is clicked on then switch between Ascending and Descending modes
            if (value == sortExp)
                sortDirection = (sortDirection == 'ASC')? 'DESC' : 'ASC';
            else
                sortDirection = 'ASC';
                sortExp = value;
            }
    }
   
    //Logic to sorting in asce/desce
    public String getSortDirection(){
    //if not column is selected
        if (sortExpression == null || sortExpression == '')
            return 'ASC';
        else
            return sortDirection;
    }
   
    //logic to view sorted data
    public PageReference ViewData() {
        //build the full sort expression
        string sortFullExp = sortExpression  + ' ' + sortDirection;
       
        //query the database based on the sort expression
        try{
            String RecTypeName = 'Education Sponsor';
            OpportunityRecordTypes__c edSponsorSort = OpportunityRecordTypes__c.getValues('RT1');//object to get record type using custom settings
           
            if(RecTypeName == 'Education Sponsor'){
                RecTypeName  = edSponsorSort.RecordTypeName__c;
            }
           
            oppList = Database.query('Select Id, Name, Account.Id, Account.Name, StageName, Sponsorship_For__c, Std__c, Student_Name__c, Sponsorship_Date__c,Student_Name__r.STD__c,Standard__c FROM  Opportunity WHERE Recordtype.name = :RecTypeName ORDER BY ' + sortFullExp + ' limit 1000');
        }catch(Exception e ) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING, e.getMessage() ));
        }
       
        return null;
    }
       
    //Controller
    public studentSponsorController(){
       
        try{
            OpportunityRecordTypes__c edSponsor = OpportunityRecordTypes__c.getValues('RT1');//object to get record type using custom settings
            oppList = [Select Id, Name, Account.Id, Account.Name, Record_Type_Name__c,
                       StageName,
                       Sponsorship_For__c, Std__c, Student_Name__c, Sponsorship_Date__c,
                       Student_Name__r.STD__c,Standard__c
                       FROM
                       Opportunity
                       WHERE
                       Recordtype.name = :edSponsor.RecordTypeName__c LIMIT 1000];
           
            oppObj = new Opportunity();
            con = new Contact();
            if(Apexpages.currentPage().getParameters().get('Export') != null){
                isExport = true;
            }
            else{
                isExport = false;
            }
           
        }catch(Exception e ) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING, e.getMessage() ));
        }
       
    }
   
    //Logic to view respective opportunity record
    public PageReference View() {
       Opportunity oppTemp = new Opportunity(id=ApexPages.currentPage().getParameters().get('Id'));
       PageReference stundentSponsorPage = new ApexPages.StandardController(oppTemp).view();
       return stundentSponsorPage ;
    }
   
    //Getting list of all opprtunites
    public List<Opportunity> getOppList() {
        return oppList;
    }
   
    //logic to reset searched values
    public void reset() {
        oppObj = new Opportunity();
    }
   
    //method to get search results
    public void dynamicSearch(){
        try{
            String searchingRecTypeName = 'Education Sponsor';
            OpportunityRecordTypes__c edSponsorSearch = OpportunityRecordTypes__c.getValues('RT1');//object to get record type using custom settings
           
            if(searchingRecTypeName == 'Education Sponsor'){
                searchingRecTypeName = edSponsorSearch.RecordTypeName__c;
            }
           
            String selectStr =' Select Id, Name, Account.Id, Account.Name,Standard__c,Record_Type_Name__c,Student_Name__r.STD__c, StageName, Sponsorship_For__c, Std__c, Student_Name__c, Sponsorship_Date__c ' +
                              ' FROM Opportunity WHERE Recordtype.name = :searchingRecTypeName ';
            String whereStr ='';
            String orderBy=' order by Std__c ASC limit 1000';
       
            String whereStr1 = (con.STD__c != null) ? whereStr + ' Student_Name__r.STD__c = \''+ con.STD__c+'\'' : '';
            String whereStr2 = (oppObj.Student_Name__c!=null) ? whereStr + ' Student_Name__c = \''+ oppObj.Student_Name__c+'\'' : '';
            String whereStr3 = (oppObj.Sponsorship_For__c != null) ? whereStr + ' Sponsorship_For__c = \''+ oppObj.Sponsorship_For__c+'\'' : '';       
            String whereStr4 = (oppObj.Name !='') ? whereStr + ' Name Like  \'%'+oppObj.Name+'%\' ' : '';
           
            whereStr =(whereStr1.length()>0) ? whereStr + ' AND ' + whereStr1 : whereStr;
            whereStr =(whereStr2.length()>0) ? whereStr + ' AND ' + whereStr2 : whereStr;
            whereStr =(whereStr3.length()>0) ? whereStr + ' AND ' + whereStr3 : whereStr;
            whereStr =(whereStr4.length()>0) ? whereStr + ' AND ' + whereStr4 : whereStr;
           
            String qry = (selectStr + whereStr + orderBy);
            oppList = Database.query(selectStr + whereStr + orderBy);           
        }catch(Exception e) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING, e.getMessage()));
        }
    }
   
    //method to export data into excel
    public PageReference export(){
        isExport = true;
        return null;
    }
}
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12
In this same class am not covered the below code.
In public void dynamicSearch(),

String selectStr =' Select Id, Name, Account.Id, Account.Name,Standard__c,Record_Type_Name__c,Student_Name__r.STD__c, StageName, Sponsorship_For__c, Std__c, Student_Name__c, Sponsorship_Date__c ' +
                              ' FROM Opportunity WHERE Recordtype.name = :searchingRecTypeName ';
            String whereStr ='';
            String orderBy=' order by Std__c ASC limit 1000';
       
            String whereStr1 = (con.STD__c != null) ? whereStr + ' Student_Name__r.STD__c = \''+ con.STD__c+'\'' : '';
            String whereStr2 = (oppObj.Student_Name__c!=null) ? whereStr + ' Student_Name__c = \''+ oppObj.Student_Name__c+'\'' : '';
            String whereStr3 = (oppObj.Sponsorship_For__c != null) ? whereStr + ' Sponsorship_For__c = \''+ oppObj.Sponsorship_For__c+'\'' : '';       
            String whereStr4 = (oppObj.Name !='') ? whereStr + ' Name Like  \'%'+oppObj.Name+'%\' ' : '';
           
            whereStr =(whereStr1.length()>0) ? whereStr + ' AND ' + whereStr1 : whereStr;
            whereStr =(whereStr2.length()>0) ? whereStr + ' AND ' + whereStr2 : whereStr;
            whereStr =(whereStr3.length()>0) ? whereStr + ' AND ' + whereStr3 : whereStr;
            whereStr =(whereStr4.length()>0) ? whereStr + ' AND ' + whereStr4 : whereStr;
           
            String qry = (selectStr + whereStr + orderBy);
            oppList = Database.query(selectStr + whereStr + orderBy);


Thanks in advance...
Pradeep RajuPradeep Raju

PageReference pg = page.StudentSponsorPage;
Test.setCurrentPage(pg);

ApexPages.CurrentPage().getParameters().put('Export',sfdc.tabName);

studentSponsorController ssc=new studentSponsorController();

Since it is in Constructor no need to call any method explicitly 
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12

ok thank u,

forget about that,
can you cover the code for this?

In this same class am not covered the below code.
In public void dynamicSearch(),

String selectStr =' Select Id, Name, Account.Id, Account.Name,Standard__c,Record_Type_Name__c,Student_Name__r.STD__c, StageName, Sponsorship_For__c, Std__c, Student_Name__c, Sponsorship_Date__c ' +
                              ' FROM Opportunity WHERE Recordtype.name = :searchingRecTypeName ';
            String whereStr ='';
            String orderBy=' order by Std__c ASC limit 1000';
      
            String whereStr1 = (con.STD__c != null) ? whereStr + ' Student_Name__r.STD__c = \''+ con.STD__c+'\'' : '';
            String whereStr2 = (oppObj.Student_Name__c!=null) ? whereStr + ' Student_Name__c = \''+ oppObj.Student_Name__c+'\'' : '';
            String whereStr3 = (oppObj.Sponsorship_For__c != null) ? whereStr + ' Sponsorship_For__c = \''+ oppObj.Sponsorship_For__c+'\'' : '';      
            String whereStr4 = (oppObj.Name !='') ? whereStr + ' Name Like  \'%'+oppObj.Name+'%\' ' : '';
          
            whereStr =(whereStr1.length()>0) ? whereStr + ' AND ' + whereStr1 : whereStr;
            whereStr =(whereStr2.length()>0) ? whereStr + ' AND ' + whereStr2 : whereStr;
            whereStr =(whereStr3.length()>0) ? whereStr + ' AND ' + whereStr3 : whereStr;
            whereStr =(whereStr4.length()>0) ? whereStr + ' AND ' + whereStr4 : whereStr;
          
            String qry = (selectStr + whereStr + orderBy);
            oppList = Database.query(selectStr + whereStr + orderBy);


Thanks in advance...

Pradeep RajuPradeep Raju
You should have a Dummy Opportunity record in your test class where Recordtype.name = :searchingRecTypeName 
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12
How can i cover the code?
can you write the code for me?

Thanks in advance.
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12
Can any one tell me
How can i cover the code for my controller?

In public void dynamicSearch(),

String selectStr =' Select Id, Name, Account.Id, Account.Name,Standard__c,Record_Type_Name__c,Student_Name__r.STD__c, StageName, Sponsorship_For__c, Std__c, Student_Name__c, Sponsorship_Date__c ' +
                              ' FROM Opportunity WHERE Recordtype.name = :searchingRecTypeName ';
            String whereStr ='';
            String orderBy=' order by Std__c ASC limit 1000';
     
            String whereStr1 = (con.STD__c != null) ? whereStr + ' Student_Name__r.STD__c = \''+ con.STD__c+'\'' : '';
            String whereStr2 = (oppObj.Student_Name__c!=null) ? whereStr + ' Student_Name__c = \''+ oppObj.Student_Name__c+'\'' : '';
            String whereStr3 = (oppObj.Sponsorship_For__c != null) ? whereStr + ' Sponsorship_For__c = \''+ oppObj.Sponsorship_For__c+'\'' : '';     
            String whereStr4 = (oppObj.Name !='') ? whereStr + ' Name Like  \'%'+oppObj.Name+'%\' ' : '';
         
            whereStr =(whereStr1.length()>0) ? whereStr + ' AND ' + whereStr1 : whereStr;
            whereStr =(whereStr2.length()>0) ? whereStr + ' AND ' + whereStr2 : whereStr;
            whereStr =(whereStr3.length()>0) ? whereStr + ' AND ' + whereStr3 : whereStr;
            whereStr =(whereStr4.length()>0) ? whereStr + ' AND ' + whereStr4 : whereStr;
         
            String qry = (selectStr + whereStr + orderBy);
            oppList = Database.query(selectStr + whereStr + orderBy);

can you write the  test class code for me for covering the above code?

Thanks in advance.
Pradeep RajuPradeep Raju
studentSponsorController ssc=new studentSponsorController();

ssc.dynamicSearch();
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12
yes praddeep, i called that method,but some of the code only covered,
the uncovered code am displayed.
for that i want code coverage..

Thanks in advance.


Pradeep RajuPradeep Raju
By the way who developed that orginal class ?