0
public with sharing class accountsearch {
    public String searchvalue{set;get;}

    public string mymessages{get; set;}
        public String searchtext { get; set; }
    public Boolean val{get; set;}
    public string phonetext {get;set;}
                    public accountsearch(ApexPages.StandardController controller) {
                        val=false;
                        //val=false;
                        if(searchtext.isNumeric())
                        {
                            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'dont allow numbers'));
                        }
                        phonevalue = '%phonetext%';
                        searchvalue = '%searchtext%';
                        }
    public string phonevalue{get; set;}
                public PageReference search() {
                    val=true;
                    acclist = new list<Account> ();
                    phonevalue = '%'+phonetext+'%';
                    searchvalue = '%'+searchtext+'%';
                    system.debug('seinm'+phonevalue+'sa'+searchvalue);
                    acclist = [select Id,Name,Website,Phone,Fax from Account where Name like: searchvalue  or Phone like: phonevalue  ];
                        system.debug('damn'+phonevalue+'sa'+searchvalue);
                            return null;
                }
    public List<Account> acclist{set;get;}
    }

for this,i wrote a test class like following,

@isTest
    public class Testaccountsearch{
    @isTest
            static void testing(){
                    Account a=new Account();
                    //ApexPages.StandardController controller=new ApexPages.StandardController(a);
                    accountsearch acse=new accountsearch(new ApexPages.StandardController(a));
                    String searchvalue=string.ValueOf(21);
                    string mymessages='error message';
                    String searchtext='itya11';
                    Boolean val=true;
                    string phonetext='1234';
                    string phonevalue='345';
                    acse.search();
            }
    }

but my test class giving error,attempt to dereference null,at line 7,i.e,while creating reference to that class passing standard controller

6
  • insert account in your test class. Mar 9 '16 at 8:09
  • inserted account,but not working.
    – Satya
    Mar 9 '16 at 8:58
  • share your updated code. Mar 9 '16 at 9:01
  • @isTestpublic class Testaccountsearch{@isTest static void testing(){ Account a=new Account(Name='stackexchange');insert a; //ApexPages.StandardController controller=new ApexPages.StandardController(a); accountsearch acse=new accountsearch(new ApexPages.StandardController(a)); String searchvalue=string.ValueOf(21); string mymessages='error message';String searchtext='itya11';Boolean val=true;string phonetext='1234';string phonevalue='345';acse.search();} }
    – Satya
    Mar 9 '16 at 9:37
  • Still in this line you are getting error //ApexPages.StandardController controller=new ApexPages.StandardController(a); Mar 9 '16 at 9:41
0

I think you are in the right direction. You have instantiated an account however it does not contains any mandatory details and is not inserted. You need to perform a DML to insert the account you have created in line 5.

Also after creating the instance, you need need to provide the inputs that were to be provided by the user on the test screen.

@isTest
    public class Testaccountsearch{
    @isTest
            static void testing(){
                    Account a=new Account();
                    a.Name = 'Test Account For SomeRandomCause';
                    insert a;
                    Test.startTest();
                    accountsearch acse=new accountsearch(new ApexPages.StandardController(a));

                    acse.searchvalue=string.ValueOf(21);
                    acse.searchtext='itya11';
                    acse.phonetext='1234';

                    acse.search();
                    Test.stopTest();
            }
    }

I recommend reading Apex Testing Trailhead.

I believe that line number 15-16 are not necessary in you constructor.

phonevalue = '%phonetext%';
searchvalue = '%searchtext%';

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.