-1

I am starting to learn writing testclasses but dont know

how to assert on a standard controller

see my code below

public class accSearchController {

    public string searchStringContact {get;set;}
    public string searchStringPhone {get;set;}
    public list<account> accList{set;get;}
    public List<Contact> contactList{get;set;}
    private string searchString='';
    private string searchVar='';

    private ApexPages.StandardController std;
    public accSearchController(ApexPages.StandardController stdCtrl) {
        std=stdCtrl;
    }

    public void search(){
        IF(searchStringContact!=''){
            searchString= searchStringContact;
            searchVar='lastname';
        }ELSE{searchString= searchStringPhone;
            searchVar='Phone';
        }  

        string searchquery='SELECT id,Name,(SELECT id,lastname,firstname,Phone,LeadSource,DoNotCall FROM Contacts )FROM Account WHERE id IN (Select AccountId from contact WHERE '+searchVar+' LIKE \'%'+searchString+'%\') Limit 5';
        accList= Database.query(searchquery); 
        system.debug('accList '+accList);       
    }
}

// test class

@isTest
private class accSearchControllerTestClass {

    @isTest  
    private static void testsearch() 
    { 
        account testccount;
        string searchStringContact='';
        //Create searchstring
        ApexPages.StandardController sc = new                 
        ApexPages.standardController(testccount);
        //ApexPages.currentPage().getParameters().put('id', 'test');
        accSearchController myPageCon = new accSearchController(sc);

        System.assertEquals(myPageCon,null);   
    }
}
  • Can you add the problem you facing? And what help you need? – jagmohan Sep 6 '17 at 9:03
0

Apex_testing

Writing Efficient Test Classes

@isTest
public class accSearchControllerTestClass {
    @isTest  
    private static void testsearch() 
    { 
        List<account> accounts=new list<account>();
        accounts.add(new account(Name='account1'));
        accounts.add(new account(Name='account2'));
        accounts.add(new account(Name='account3'));
        insert accounts;

        list<contact> contacts=new list<contact>();                
        contacts.add(new contact(AccountId=accounts[0].id,lastname='contact1LastName',firstname='Contact1FirstName',Phone=9876543210,LeadSource='web',DoNotCall__c=''));
        contacts.add(new contact(AccountId=accounts[0].id,lastname='contact2LastName',firstname='Contact2FirstName',Phone=9876543211,LeadSource='web',DoNotCall__c=''));
        contacts.add(new contact(AccountId=accounts[1].id,lastname='contact3LastName',firstname='Contact3FirstName',Phone=9876543211,LeadSource='web',DoNotCall__c=''));
        Insert Contacts;


        ApexPages.StandardController sc = new ApexPages.standardController(accounts[2]);
        accSearchController myPageCon = new accSearchController(sc);
        myPageCon.searchStringContact='contact1LastName';        
        myPageCon.search();
        System..assertNotEquals(myPageCon.accList.size(),1);  
        myPageCon.searchStringContact=''; 
        myPageCon.searchStringPhone ='9876543211';
        myPageCon.search();
        System..assertNotEquals(myPageCon.accList.size(),2);  
    }
}

}
0

The focus of your test should be the behaviour of the code you have written. Most of what you need to know is described in Testing Custom Controllers and Controller Extensions.

For your current case:

// Your test method needs to insert the data that it is going to search for
Account account = new Account(Name = 'Acme');
insert account;
Contact contact = new Contact(
        FirstName = 'Jane',
        LastName = 'Doe',
        AccountId = account.Id
        );
insert contact;

accSearchController c = new accSearchController(sc);
c.searchStringContact = 'Doe';
c.search();
System.assertEquals(1, c.accList.size();
// Add more asserts on c.accList

Also to avoid the risk of SOQL Injection use escapeSingleQuotes in your controller extension.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.