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
ranjith1.3929645002009956E12ranjith1.3929645002009956E12 

Test class Code coverage for this code of snippet

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()));
        }
    }
In the above dynamicSearch() method some of the code(iam displaying below) not covered.

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 tell me how can we cover the code for that.


Here is my test class,
can you tell me how can we cover the code for that?


//Test class for StudentSponsorController
  
     static testMethod void testStudentSponsorController(){
      
        List<Opportunity> allOppotunityList = new List<Opportunity>();
        String searchingRecTypeName;
      
        Account acc = new Account(Name = 'Test Account');
        insert acc;
      
        Contact conTemp  = new Contact();
        conTemp.LastName = 'Test Contact';
        conTemp.STD__c = '12';
        insert conTemp;
      
        Opportunity oppTemp = new Opportunity(Name = 'testOpp11');
        oppTemp.AccountId = acc.Id;
        oppTemp.name='menaka';
        oppTemp.Student_Name__c = conTemp.Id;
        oppTemp.Std__c='8';
        oppTemp.Record_Type_Name__c='Education Sponsor';
        oppTemp.CloseDate = System.today();
        oppTemp.StageName = 'Closed Won';
        oppTemp.Sponsorship_For__c='Education';
        oppTemp.Sponsorship_Date__c=System.today()+2;
        insert oppTemp;
      
                  
        allOppotunityList.add(oppTemp);
      
        PageReference pg = page.StudentSponsorPage;
        Test.setCurrentPage(pg);

        ApexPages.CurrentPage().getParameters().put('Export','sfdc.tabName');
      
        studentSponsorController stdntSC = new studentSponsorController();
        stdntSC.sortDirection='ASC';
        stdntSC.sortExp='ASC';
        stdntSC.sortExpression=stdntSC.sortExp;
        stdntSC.getSortDirection();
        stdntSC.ViewData();
        stdntSC.View();
        stdntSC.getOppList();
        stdntSC.reset();
        stdntSC.dynamicSearch();
        stdntSC.export();
      
        Opportunity oppTemp1 = new Opportunity(Name = 'opptest2');
        oppTemp1.AccountId = acc.Id;
        oppTemp1.name='menaka';
        oppTemp1.Student_Name__c = conTemp.Id;
        oppTemp1.CloseDate = System.today()+3;
        oppTemp1.StageName = 'Posted';
        searchingRecTypeName ='Education Sponsor';
        insert oppTemp1;
      
                  
        allOppotunityList.add(oppTemp1);
     
       studentSponsorController stdntSC1 = new studentSponsorController();
        stdntSC1.sortDirection='';
        stdntSC1.sortExp='';
        stdntSC1.sortExpression=stdntSC.sortExp;
        stdntSC1.getSortDirection();
        stdntSC1.ViewData();
        stdntSC1.View();
        stdntSC1.getOppList();
        stdntSC1.reset();
        oppTemp1.Record_Type_Name__c= '';
        stdntSC1.dynamicSearch();
        stdntSC1.export();
    }

Thank you in advance.
Abhi_TripathiAbhi_Tripathi
Hey,

Go for this post, it will surely helps you out for writing a test class

http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html 

Regards,
Abhi Tripathi
Salesforce Developer