+ Start a Discussion
Jean Grey 10Jean Grey 10 

Test Class for Search Page

I'm having a hard time with this test class - seems simple enough but I'm getting a "Constructor not defined" error. Can anyone help?

Class:
public class mtaSearch{

    public Date startDate {get;set;}
	public Date endDate {get;set;}
	public String vendor {get;set;}
	public String accName {get;set;}    
    public String searchQuery {get;set;}
    public String leadQuery {get;set;}
    public String accountQuery {get;set;}
    public Integer querySize {get;set;}
    public Integer leadQuerySize {get;set;}
    public String searchString {get;set;}
    public List<Account> accList {get;set;}
    public Set<Id> accSet {get;set;}
    public List<Lead> leadList {get;set;}    
    public Set<Id> leadSet {get;set;}
    public List<Contact> conList {get;set;}
    public List<Contact> conList1 {get;set;}
    public Set<Id> conSet {get;set;}
    public String contactQuery {get;set;}
    public Integer contactQuerySize {get;set;}
    public String contactQuery1 {get;set;}
    public List<OpportunityContactRole> ocrList {get;set;}
    public Set<Id> ocrSet {get;set;}
    public Integer ocrSize {get;set;}
	public List<Task> taskSearch {get;set;}
	public List<CampaignMember> cmSearch {get;set;}
	public List<Campaign> campSearch {get;set;}
	public List<Opportunity> oppList {get;set;}
	public Set<Id> oppSet {get;set;}
    public Integer oppSize {get;set;}    
	public List<Opportunity> wonOppList {get;set;}
	public Set<Id> wonOppSet {get;set;}
    public Integer wonOppSize {get;set;}
	public List<Lead> cvList {get;set;}
	public Set<Id> cvSet {get;set;}
    public Integer cvSize {get;set;}
    public String cvLeadQuery {get;set;}
    
	public List<Task> taskList {get;set;}
	public Set<Id> taskSet {get;set;}
	public Date todayDate {get;set;}
    public object totalWon {get;set;}
    public String sWon {get;set;}
    public Decimal QTotal {get;set;}
    public List<String> argsTotal{get;set;}
    
	//set up controller & extension for vf page
    ApexPages.StandardSetController setCon;
    public mtaSearch(ApexPages.StandardSetController controller) {
        setCon = controller;
    }

    public void search(){
        //set up string to display $
		argsTotal= new String[]{'0','number','###','###','##0.00'};
    	//set up date variable
		todayDate = system.today();
    	if(startDate==NULL){startDate=todayDate-90;}
    	if(endDate==NULL){endDate=todayDate;}
    	accountQuery = 'SELECT Name,Id from Account where Name LIKE \'%'+accName+'%\' LIMIT 50000';
        accList = Database.query(accountQuery);
        querySize = accList.size();
        accSet = new Set<Id>();
        if(accList.size()>0){
            for(Account a:accList){accSet.add(a.Id);}}
        leadQuery = 'SELECT Name,Id from Lead where (Company LIKE \'%'+accName+'%\' OR Origination__c LIKE \'%'+vendor+'%\') AND IsConverted =FALSE LIMIT 50000';
        leadList = Database.query(leadQuery);
        leadQuerySize = leadList.size();
        leadSet = new Set<Id>();
        if(leadList.size()>0){
            for(Lead l:leadList){leadSet.add(l.Id);}}
        cvLeadQuery = 'SELECT Name,Id,ConvertedContactId from Lead where (Company LIKE \'%'+accName+'%\' OR Origination__c LIKE \'%'+vendor+'%\') AND IsConverted =TRUE LIMIT 50000';
        cvList = Database.query(cvLeadQuery);
        cvSize = cvList.size();
        cvSet = new Set<Id>();
        if(cvList.size()>0){
            for(Lead l:cvList){leadSet.add(l.Id);}}
        conList = new List<Contact>([SELECT Name, Id FROM Contact WHERE Id IN:cvSet]);
        conSet = new Set<Id>();
        if(conList.size()>0){
            for(Contact c:conList){conSet.add(c.Id);}}
        system.debug('conSet '+conSet);
		contactQuery1 = 'SELECT Name,Id from Contact where (Account.Name LIKE \'%'+accName+'%\' OR Origination__c LIKE \'%'+vendor+'%\') LIMIT 50000';
        system.debug('contactQuery1 '+contactQuery1);
        conList1 = Database.query(contactQuery1);
        system.debug('conList1 '+conList1);
        if(conList1.size()>0){
            for(Contact c:conList1){conSet.add(c.Id);}}
        system.debug('conSet '+conSet);
        contactQuerySize = conSet.size();        
        ocrList = new List<OpportunityContactRole> ([SELECT Id,OpportunityId,ContactId,Contact.AccountId,Opportunity.StageName FROM OpportunityContactRole WHERE ContactId IN :conSet LIMIT 50000]);
        oppSet = new Set<Id>();
        if(ocrList.size()>0){
            for(OpportunityContactRole ocr :ocrList){
                oppSet.add(ocr.OpportunityId);
                oppSize = oppSet.size();
            }}
        wonOppList = new List<Opportunity>([SELECT Id, Owner.UserRole.Name, Original_Lead_Source__c, Name,StageName, Amount,OwnerId, Owner.Name,CreatedDate,CloseDate FROM Opportunity WHERE StageName = 'Closed–Won' AND Id IN :oppSet LIMIT 50000]);
        wonOppSet = new Set<Id>();
        if(wonOppList.size()>0){
            for(Opportunity o:wonOppList){wonOppSet.add(o.Id);}
        wonOppSize = wonOppSet.size();
        //get total amount of won opps for this search
        List<AggregateResult> wonAgg = new List<AggregateResult> ([SELECT Sum(Amount) 
                                                                         FROM Opportunity 
                                                                         WHERE Id IN :wonOppSet]);
            if(wonAgg.size()>0){
                totalWon = wonAgg[0].get('expr0');}
        if(totalWon==NULL){QTotal=0;}
            if(integer.valueOf(totalWon)>0){
            QTotal = integer.valueOf(totalWon);}
            sWon = String.format(qTotal.format(), argsTotal);}
        }

Test:
@isTest(seeAllData=true)

public class mtaSearchTest {

    static testMethod void validateSearch(){
        Test.startTest();

        DateTime dateNow = system.now();
        Date dateToday = system.today();

        Account acct = new Account(
            name = 'Test Account'
            );
        insert acct;

        Contact cont = new Contact(
            accountId = acct.id,
            firstName = 'Test',
            lastName = 'Contact',
            title = 'Test'
            );
        insert cont;
        
        Opportunity opp = new Opportunity(
            Name = 'Test Opp',
            StageName = 'In Purchasing',
            CloseDate = dateToday,
            Amount = 1400,
            AccountId = acct.Id,
            LeadSource = 'Other'
            );
        insert opp;
        
        opp.StageName = 'Closed–Won';
        opp.Closed_Won_Reason__c = 'Data Breach';
        opp.Closed_Won_Explanation__c = 'Test';
        update opp;
        
        Opportunity oppLost = new Opportunity(
        Name = 'Test Opp',
            StageName = 'In Purchasing',
            CloseDate = dateToday,
            Amount = 1500,
            AccountId = acct.Id,
            LeadSource = 'Other');
        insert oppLost;
                
        ID sysAdm = [SELECT Id FROM Profile WHERE Name = 'System Administrator'].Id;
		//user test data
		List<User> userList = new List<User>();
		User u = new User(ProfileId = sysAdm,LastName = 'last',Email = 'testuser@test.com',
                          Username = 'testuser@test.com' + System.currentTimeMillis(),
                          CompanyName = 'TEST',Title = 'title',Alias = 'alias',
                          TimeZoneSidKey = 'America/Los_Angeles',EmailEncodingKey = 'UTF-8',
                          LanguageLocaleKey = 'en_US',LocaleSidKey = 'en_US');
		userList.add(u);
        insert userList;
        
        PageReference pageRef = Page.mtaSearch;
        pageRef.getParameters().put('query', 'test');
        Test.setCurrentPage(pageRef);

//this is where i get the error
		mtaSearch doSearch=new mtaSearch();
	    doSearch.search();

        
    }
}

 
Best Answer chosen by Jean Grey 10
Raj VakatiRaj Vakati
Modify the code as shown below 
 
PageReference pageRef = Page.Page1;
        pageRef.getParameters().put('query', 'test');
        Test.setCurrentPage(pageRef);
        
        //this is where i get the error
        
        List<Opportunity> opps = new List<Opportunity>();
        opps.add( [ Select Id From Opportunity ] );
        
        ApexPages.StandardSetController sc = new ApexPages.StandardSetController(opps);
        mtaSearch doSearch=new mtaSearch(sc);
        doSearch.search();



 

All Answers

Raj VakatiRaj Vakati
Modify the code as shown below 
 
PageReference pageRef = Page.Page1;
        pageRef.getParameters().put('query', 'test');
        Test.setCurrentPage(pageRef);
        
        //this is where i get the error
        
        List<Opportunity> opps = new List<Opportunity>();
        opps.add( [ Select Id From Opportunity ] );
        
        ApexPages.StandardSetController sc = new ApexPages.StandardSetController(opps);
        mtaSearch doSearch=new mtaSearch(sc);
        doSearch.search();



 
This was selected as the best answer
Raj VakatiRaj Vakati
You need Use  StandardSetController   controller and passit the constructor 
Jean Grey 10Jean Grey 10
Thank you Raj, this worked perfectly.