0

Code for the controller

public class AccountSearchControllerNew {

    public String varPhone{get;set;}
    public String varEmail{get;set;}
    public String varStore{get;set;}
    public Boolean searchFlag{get;set;}
    public Boolean editFlag{get;set;}   
    public Boolean SelectSrcCrtFlag{get;set;}
    public Account newAccount{get;set;}
    public List<Account> accList{get;set;}
    Id rtIDAcc;
    public String rtAccName{get;set;}
    public string ownerName{get;set;}
    string queryList;
    public Boolean phoneError{get;set;}
    public Boolean emailError{get;set;}
    public Boolean storeError{get;set;}

    public AccountSearchControllerNew(){
        phoneError = false;
        emailError = false;
        storeError = false;
        SelectSrcCrtFlag = true;
        searchFlag = false;
        editFlag = false;
        accList = new List<Account>();
        Map<string,schema.RecordTypeInfo> rtName = schema.SObjectType.Account.getRecordTypeInfosByName();
        rtIDAcc = rtName.get('Lead').getRecordTypeId();
        rtAccName = rtName.get('Lead').getName();
        ownerName = UserInfo.getName();
    }

    public void searchAccount(){
        try{            
            editFlag = false;
            emailError = false;
            storeError = false;
            accList = new List<Account>();
            string lastEightDigits;
            //queryList = 'Select Id,LastName,Name,IsPersonAccount,Store_Name__c,Phone,PersonMobilePhone,Subscription_Email__c,Alternate_Email__c,OwnerId,RecordTypeId,RecordType.Name from Account';
            if(varPhone != null && varPhone.trim().length()>0){

                SelectSrcCrtFlag = false;
                if(varPhone.length() >= 8){
                lastEightDigits = varPhone.substring(varPhone.length()-8,varPhone.length()); 
                system.debug('--lastEightDigits--'+lastEightDigits);
                //queryList += ' where (Phone Like \'%'+varPhone+'%\' OR PersonMobilePhone Like \'%'+varPhone+'%\')';
                //queryList += 'Select Id,LastName,Name,IsPersonAccount,Store_Name__c,Phone,PersonMobilePhone,Subscription_Email__c,Alternate_Email__c,OwnerId,RecordTypeId,RecordType.Name from Account where (Phone Like \'%'+varPhone+'%\' OR PersonMobilePhone Like \'%'+varPhone+'%\')';
                //system.debug('--queryList--'+queryList); 
                //accList = Database.query(queryList); 
                accList = [Select Id,FirstName,LastName,Name,IsPersonAccount,Store_Name__c,Phone,PersonMobilePhone,Subscription_Email__c,Alternate_Email__c,OwnerId,RecordTypeId,RecordType.Name from Account where (Phone Like :'%'+lastEightDigits OR PersonMobilePhone Like :'%'+lastEightDigits)];
                system.debug('--accList--'+accList);

                if(accList == null || accList.size() == 0){
                    emailError = false;
                    phoneError = true;
                    searchFlag = false;
                    storeError = false;
                }
                else{
                        searchFlag = true;
                        phoneError = false;
                        emailError = false;
                        storeError = false;
                    }
                }
                else{
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Please enter valid Phone Number.'));
                }

            }
            else{
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Please enter Phone Number.'));
            }

        }
        catch(Exception e){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
        }
    }

    public void searchAccountByEmail(){
        try{
            phoneError = false;
            storeError = false;
            editFlag = false;
            accList = new List<Account>();
            //queryList = 'Select Id,LastName,Name,IsPersonAccount,Store_Name__c,Phone,PersonMobilePhone,Subscription_Email__c,Alternate_Email__c,OwnerId,RecordTypeId,RecordType.Name from Account';
            if(varEmail != null && varEmail.trim().length()>0){
                if(!Pattern.matches('[a-zA-Z0-9._-]+@[a-zA-Z]+.[a-zA-Z]{2,4}', varEmail))
                {
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'Please enter valid email address.'));
                }
                else{

                    SelectSrcCrtFlag = false;
                    //queryList += ' where (Phone Like \'%'+varPhone+'%\' OR PersonMobilePhone Like \'%'+varPhone+'%\')';
                    //queryList += 'Select Id,LastName,Name,IsPersonAccount,Store_Name__c,Phone,PersonMobilePhone,Subscription_Email__c,Alternate_Email__c,OwnerId,RecordTypeId,RecordType.Name from Account where (Phone Like \'%'+varPhone+'%\' OR PersonMobilePhone Like \'%'+varPhone+'%\')';
                    //system.debug('--queryList--'+queryList);
                    //accList = Database.query(queryList); 
                    accList = [Select Id,FirstName,LastName,Name,IsPersonAccount,Store_Name__c,Phone,PersonMobilePhone,Subscription_Email__c,Alternate_Email__c,OwnerId,RecordTypeId,RecordType.Name from Account where (Subscription_Email__c Like :varEmail OR Alternate_Email__c Like :varEmail)];
                    system.debug('--accList--'+accList);
                    if(accList == null || accList.size() == 0){
                        phoneError = false;
                        emailError = true;
                        searchFlag = false;
                        storeError = false;
                    }
                    else{
                        phoneError = false;
                        emailError = false;
                        searchFlag = true;
                        storeError = false;
                    }
                }
            }
            else{
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Please enter Email Address.'));
            }

        }
        catch(Exception e){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
        }
    }

    public void searchAccountByStore(){
        try{
            editFlag = false;
            accList = new List<Account>();
            //queryList = 'Select Id,LastName,Name,IsPersonAccount,Store_Name__c,Phone,PersonMobilePhone,Subscription_Email__c,Alternate_Email__c,OwnerId,RecordTypeId,RecordType.Name from Account';
            if(varStore != null && varStore.trim().length()>0){
                //searchFlag = true;
                SelectSrcCrtFlag = false;
                //queryList += ' where (Phone Like \'%'+varPhone+'%\' OR PersonMobilePhone Like \'%'+varPhone+'%\')';
                //queryList += 'Select Id,LastName,Name,IsPersonAccount,Store_Name__c,Phone,PersonMobilePhone,Subscription_Email__c,Alternate_Email__c,OwnerId,RecordTypeId,RecordType.Name from Account where (Phone Like \'%'+varPhone+'%\' OR PersonMobilePhone Like \'%'+varPhone+'%\')';
                //system.debug('--queryList--'+queryList);
                //accList = Database.query(queryList); 
                accList = [Select Id,FirstName,LastName,Name,IsPersonAccount,Store_Name__c,Phone,PersonMobilePhone,Subscription_Email__c,Alternate_Email__c,OwnerId,RecordTypeId,RecordType.Name from Account where (Store_Name__c Like :varStore)];
                system.debug('--accList--'+accList);
                if(accList == null || accList.size() == 0){
                    emailError = false;
                    phoneError = false;
                    storeError = true;
                    searchFlag = false;
                }
                else{
                        phoneError = false;
                        emailError = false;
                        storeError = false;
                        searchFlag = true;
                    }
            }
            else{
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Please enter Store Name Number.'));
            }

        }
        catch(Exception e){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
        }
    }

    public void createNewAcc(){
        try{
            editFlag = true;
            searchFlag = false;
            SelectSrcCrtFlag = false;
            phoneError = false;
            emailError = false;
            storeError = false;

            newAccount = new Account();
            newAccount.RecordTypeId = rtIDAcc;
            if(varPhone != null && varPhone.trim().length()>0){
                newAccount.Phone= varPhone;
                newAccount.LastName = 'LastName #'+varPhone;
            }
            else{
                newAccount.LastName = 'LastName #';
            }   
            if(varEmail != null && varEmail.trim().length()>0)
                newAccount.Subscription_Email__c = varEmail;
            if(varStore != null && varStore.trim().length()>0)   
                newAccount.Store_Name__c = varStore;
        }
        catch(Exception e){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
        }        
    }

    public PageReference saveNewAcc(){
        try{
            insert newAccount;
            PageReference accPageRef = new PageReference('/' + newAccount.id);
            accPageRef.setRedirect(true);
            return accPageRef;
        }
        catch(Exception e){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
        }
        return null;
    }

    public PageReference cancelCreation(){
        try{
            /*PageReference accPageRef = new PageReference('/apex/Inboundcallpage');
            accPageRef.setRedirect(true);
            return accPageRef;*/
            editFlag = false;
            return null;
        }
        catch(Exception e){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
        }
        return null;
    }

}

My Test Class -

@isTest
public class TestAccountSearchControllerNew {
    static Testmethod void AccountSearchControllerNew()
    {
        RecordType  rt= [Select Id,Name From RecordType Where SobjectType = 'Account' and Name ='Lead'];
        RecordType  rt1= [Select Id,Name From RecordType Where SobjectType = 'Account' and Name ='Merchant'];
        Account A = new Account();
        A.FirstName ='Mack';
        A.LastName='Rodney';
        A.Store_Name__c='Hamleys';
        A.Phone='9876854758';
        A.PersonMobilePhone='7894561230';
        A.Subscription_Email__c='A@gmail.com';
        A.Alternate_Email__c='B@gmail.com';
        insert A;

        Account accnew=new Account();
        accnew.FirstName='test';
        accNew.LastName='TMC';
        //accNew.IsPersonAccount=true;
        accNew.Store_Name__c='testTMC';
        accNew.Phone='98732173';
        accNew.PersonMobilePhone='73217388';
        accNew.Subscription_Email__c='test@gmail.com';
        accNew.Alternate_Email__c='test12@gmail.com';
        accNew.RecordTypeId=rt.Id;
        insert accNew;

        PageReference pageRef = Page.Inboundcallpage;
        Test.setCurrentPage(pageRef);
        AccountSearchControllerNew Acc = new AccountSearchControllerNew();
         Acc.searchAccountByStore();
         Acc.searchAccountByEmail();
         Acc.searchAccount();
         Acc.createNewAcc();
         Acc.saveNewAcc();
         Acc.editFlag=true;
         Acc.varPhone='9876543210';
         Acc.varEmail='test@test.org';
         Acc.varStore='H&M';
         Acc.searchFlag=False;
         Acc.SelectSrcCrtFlag=True;
         Acc.rtAccName='Test';
         Acc.ownerName='Frank';
         Acc.phoneError=true;
         Acc.emailError=false;
         Acc.storeError=true; 
}}

Kindly help!!!

Screenshot of lines not covered

  • Can you provide more information about which lines aren't being covered? What have you tried? Do you have conditions in your testMethods that test each branch of logic in your controller? It will also make it easier for people to help you if you remove commented out lines of code. There's no colour coding here – Nick Cook Oct 20 '15 at 6:14
  • @NickCook I have added a screenshot for the lines that aren't covered. – Gaurav Arora Oct 20 '15 at 6:29
  • 1
    Don't write tests just to get code coverage. You are calling too many methods from your single test method. – smukov Oct 20 '15 at 7:51
1

When searchAccount is called, varPhone is null. So the condition on line 41 will evaluate to false.

In the last part of your test method, you assign non-null values to Acc attributes, but after that, no methods are called that use these values.

1

It is not covering your code because you have varPhone variable but you are not setting value of that variable prior to calling your method and becoming null in that case. update your code in following way in test method

 Acc.varPhone='9876543210';     
 Acc.createNewAcc();
 Acc.searchAccount();     
 Acc.saveNewAcc();
 Acc.editFlag=true;

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.