+ Start a Discussion
Redmanx03Redmanx03 

Test Class Newb

public with sharing class NewNAISearchController {
    public Comp__c compSearch {get;set;}
    public Comp__c compSearch2 {get;set;}
    public Comp__c compSearch3 {get;set;}
    public Comp__c compSearch4 {get;set;}
    
    //List of Comp
    private List <Comp__c> comps;
    public List <Comp__c> getComps() {
        List<Comp__c> listComps = (List<Comp__c>) setCon.getRecords();
        return listComps;
    }
    
    //Sort Expression
    private String sortExp = 'Name';
    public String sortExpression {
        get {
            return sortExp;
        }
        set {
            if (value == sortExp)
                sortDirection = (sortDirection == 'ASC') ? 'DESC' : 'ASC';
            else
                sortDirection = 'ASC';
            sortExp = value;
        }
    }
    
    //Sort Direction
    private String sortDirection = 'ASC';
    public String getSortDirection() {
        if (sortExpression == null || sortExpression == '')
            return 'ASC';
        else
            return sortDirection;
    }
    public void setSortDirection(String value) {
        sortDirection = value;
    }
    
    //Constructor
    public NewNAISearchController(ApexPages.StandardController controller) {
        compSearch = new Comp__c();
        compSearch2 = new Comp__c();
        compSearch3 = new Comp__c();
        compSearch4 = new Comp__c();
        whereClause();
    }
    
    //Standard set Controller
    public ApexPages.StandardSetController setCon {
        get{
            try {
                if(setCon == null) {
                    String qtQuery;
                    if(whereCondition != ''){
                        qtQuery = 'SELECT Name,Rethink2_Actual_Rate_type__c,Address__c,Average_Rental_Rate__c,Building_Class__c,Closed_Deal_Date__c,Comp_Type__c,Deal_type__c,Gross_SF__c,Zip_Code__c, SubMarket__c, Property_Type__c '
                                  +' FROM Comp__c WHERE '+ whereCondition
                                  +' ORDER BY ' + sortExpression + ' ' + sortDirection;
                    }else{
                        qtQuery = 'SELECT Name,Rethink2_Actual_Rate_type__c,Address__c,Average_Rental_Rate__c,Building_Class__c,Closed_Deal_Date__c,Comp_Type__c,Deal_type__c,Gross_SF__c,Zip_Code__c, SubMarket__c, Property_Type__c FROM Comp__c ORDER BY ' + sortExpression + ' ' + sortDirection;
                    }
                    system.debug('****'+qtQuery);
                    setCon = new ApexPages.StandardSetController(Database.Query(qtQuery));
                    setCon.setPageSize(20);   
                }
            }
            catch(Exception e) {
                Apexpages.addMessages(e);
            }
            return setCon;
        }set;
    }
    
    //Search Method
    public PageReference searchComp() {
        whereCondition = '';
        whereClause();
        setCon = null;
        return null;
    }
    

    //Method to call Next 
    public PageReference next() {
        if(setCon.getHasNext()){
            setCon.next();
        } 
        return null;
    }
    
    public Boolean hasNext {
        get {
            return setCon.getHasNext();
        }
    }
    
    //Method to call Previous 
    public Boolean hasPrevious {
        get {
            return setCon.getHasPrevious();
        }
        set;
    }
    
    public PageReference previous() {
        if(setCon.getHasPrevious()){
            setCon.previous();  
        }        
        return null;  
    }
    
    //Method to get where clause
    private string whereCondition = '';
    private Date sfClosedDate;
    private Date thruClosedDate;
    private void whereClause(){
        sfClosedDate = compSearch.Closed_Deal_Date__c;
        thruClosedDate = compSearch2.Closed_Deal_Date__c;
        if(compSearch.recordTypeId != null){
            whereCondition += ' recordTypeId =\'' + compSearch.recordTypeId +'\'';
        }
        if(compSearch.Property_Type__c != null){
            whereCondition += ' AND Property_Type__c =\'' + compSearch.Property_Type__c +'\'';
        }
        if(compSearch.Comp_Type__c != null){
            whereCondition += ' AND Comp_Type__c =\'' + compSearch.Comp_Type__c +'\'';
        }
        if(compSearch.Deal_type__c != null){
            whereCondition += ' AND Deal_type__c =\'' + compSearch.Deal_type__c +'\'';
        }
        if(compSearch.Building_Class__c != null){
            whereCondition += ' AND Building_Class__c =\'' + compSearch.Building_Class__c +'\'';
        }
        if(compSearch.Rethink2_Actual_Rate_type__c != null){
            whereCondition += ' AND Rethink2_Actual_Rate_type__c =\'' + compSearch.Rethink2_Actual_Rate_type__c +'\'';
        }
        if(compSearch.SubMarket__c != null || compSearch2.SubMarket__c != null || compSearch3.SubMarket__c != null || compSearch4.SubMarket__c != null){
            String subMarket = '';
            if(compSearch.SubMarket__c != null)
                subMarket += ' SubMarket__c =\'' + compSearch.SubMarket__c +'\'';
            if(compSearch2.SubMarket__c != null)
                subMarket += (subMarket == '' ? ' SubMarket__c =\'' + compSearch2.SubMarket__c +'\'' : ' OR SubMarket__c =\'' + compSearch2.SubMarket__c +'\'');
            if(compSearch3.SubMarket__c != null)
                subMarket += (subMarket == '' ? ' SubMarket__c =\'' + compSearch3.SubMarket__c +'\'' : ' OR SubMarket__c =\'' + compSearch3.SubMarket__c +'\'');
            if(compSearch4.SubMarket__c != null)
                subMarket += (subMarket == '' ? ' SubMarket__c =\'' + compSearch4.SubMarket__c +'\'' : ' OR SubMarket__c =\'' + compSearch4.SubMarket__c +'\'');    
            whereCondition += (subMarket == '' ? '' : ' AND (' + subMarket +')');
        }
        
        if(compSearch.Zip_Code__c != null || compSearch2.Zip_Code__c != null || compSearch3.Zip_Code__c != null || compSearch4.Zip_Code__c != null){
            String zipCode = '';
            if(compSearch.Zip_Code__c != null)
                zipCode += ' Zip_Code__c =' + compSearch.Zip_Code__c ;
            if(compSearch2.Zip_Code__c != null)
                zipCode += (zipCode == '' ? ' Zip_Code__c =' + compSearch2.Zip_Code__c  : ' OR Zip_Code__c =' + compSearch2.Zip_Code__c );
            if(compSearch3.Zip_Code__c != null)
                zipCode += (zipCode == '' ? ' Zip_Code__c =' + compSearch3.Zip_Code__c  : ' OR Zip_Code__c =' + compSearch3.Zip_Code__c );
            if(compSearch4.Zip_Code__c != null)
                zipCode += (zipCode == '' ? ' Zip_Code__c =' + compSearch4.Zip_Code__c  : ' OR Zip_Code__c =' + compSearch4.Zip_Code__c );  
            whereCondition += (zipCode == '' ? '' : ' AND (' + zipCode +')');
        }
        
        if(compSearch.Gross_SF__c != null && compSearch2.Gross_SF__c != null){
            whereCondition += ' AND ( Gross_SF__c >= '+ compSearch.Gross_SF__c +' AND Gross_SF__c <= '+compSearch2.Gross_SF__c +')';
        }else if(compSearch.Gross_SF__c != null || compSearch.Gross_SF__c != null){
            Apexpages.addMessage(new Apexpages.Message(Apexpages.severity.INFO, 'You must enter Gross between SF & Thru'));
        }
        
        if(sfClosedDate != null && thruClosedDate != null){
            whereCondition += ' AND ( Closed_Deal_Date__c >= : sfClosedDate AND Closed_Deal_Date__c <= : thruClosedDate)';
        }else if(sfClosedDate != null || thruClosedDate != null){
            Apexpages.addMessage(new Apexpages.Message(Apexpages.severity.INFO, 'You must enter Closed Deal Date between two Dates'));
        }
        
        if(compSearch.Address__c != null){
            whereCondition += ' AND Address__c =\'' + compSearch.Address__c +'\'';
        }
        
        if(compSearch.Average_Rental_Rate__c != null && compSearch2.Average_Rental_Rate__c != null){
            whereCondition += ' AND ( Average_Rental_Rate__c >= '+ compSearch.Average_Rental_Rate__c +' AND Average_Rental_Rate__c <= '+compSearch2.Average_Rental_Rate__c +')';
        }else if(compSearch.Average_Rental_Rate__c != null || compSearch2.Average_Rental_Rate__c != null){
            Apexpages.addMessage(new Apexpages.Message(Apexpages.severity.INFO, 'You must enter Min & Max Average Rental Rate'));
        }
    }
}

 HOw would i write a test class for this search controller? I am new to test classes... 

Best Answer chosen by Admin (Salesforce Developers) 
testrest97testrest97

@isTest(seeAllData=true)
private class TestClass {

static TestMethod void testMethod()
{

Comp__c c=new comp__c(..........);

ApexPages.StandardController controller = new Apexpages.Standardcontroller(c);    

NewNAISearchController nsc=new NewNAISearchController(controller);  

list<comp__c> lst=nsc.getcomps();

String x=nsc.getsortdirection();

pagereference p=nsc.searchcomp();

pagereference p1=nsc.next();

pagereference p2=nsc.previous();

boolean b=nsc.hasnext();

boolean b1=nsc.hasprevious();
}
}