1

I am new to salesforce with the test class i am facing problem please help.

this is my controller which i am going to test.

/*******************************************************************
 * Custom controller for serach functionality.
 * author : Bala & Lalit
 *******************************************************************/
public with sharing class SearchFromURLController 
{
    // Variable Declaration
    public String topass{get;set;}

    public String value{get;set;}

    public List<Account> accounts {get; set;}

    public Boolean searched {get; set;}



    //Constructor Decalaration
    public SearchFromURLController()
    {
        searched=false;
        String nameStr=ApexPages.currentPage().getParameters().get('value');
         if (null!=nameStr)
        {
            value=nameStr;
            executeSearch();
        }
    }
    // action method to call the runsearch method.
    public pageReference executeSearch()
    {
        searched=true;
        accounts = performSearch(value);
        return null;
    }

    //action method to return the values to pageblockTable.
    private List<Account> performSearch (String value)
    {
        topass ='select id, Name, Industry, Type, Rating from Account where';  
        topass = topass+'(name LIKE \'%'+ value +'%\'OR industry LIKE \'%'+ value +'%\'OR rating LIKE \'%'+ value + '%\'OR type LIKE \'%'+ value + '%\')';
        return Database.query(topass);
    }
     public pageReference edit(){
        PageReference pg = page.EditFromSearch;
        Integer idx =1;
        for(Account acc :Database.query(topass)){
            pg.getParameters().put('account' + idx, acc.id);
            idx++;
        }
        return pg;
    }
}

and my test class is this which i am writing to test the above class currently i am getting 32% code coverage :

* This test class tests the functionality of the SearchFromURLController
*/
@isTest
private class SearchFromURLControllerTest {

    //test searching when the search term appears on the URL.
    static testMethod void searchFromURLTest(){

        Test.StartTest();
        List<Account> accs=new List<Account>();
        accs.add(new Account(Name='Unit Test',Industry = 'Energy',Type ='Other',Rating = 'Hot'));
        accs.add(new Account(Name='Unit Test 2',Industry = 'Energy',Type ='Other',Rating = 'Hot'));
        accs.add(new Account(Name='The Test Account'));
        insert accs;

        PageReference pr=Page.SearchFromURL;
        pr.getParameters().put('name', 'Unit');
        pr.getParameters().put('industry', 'Energy');
        pr.getParameters().put('type', 'Hot');

        Test.setCurrentPage(pr);

        SearchFromURLControllerOriginal controller=new SearchFromURLControllerOriginal();
        System.assertEquals(true, controller.searched);
        System.assertEquals(2, controller.accounts.size());
        Test.StopTest();   
    }

    //test searching when users enters a value.
    static testMethod void executeSearchTest()
    {
           Test.StartTest();
           List<Account> acc = new List<Account>();
           acc.add(New Account(Name ='Bernard',Industry = 'Energy',Type ='Other',Rating = 'Hot'));

           insert acc;
           system.debug('value of the list in the executeSearchTest function++++++++++++'+acc);

           //pagereference instantiation
           PageReference pr=Page.SearchFromURL;
           pr.getParameters().put('name','Bernard');
           pr.getParameters().put('industry','Energy');
           pr.getParameters().put('rating','hot');
           pr.getParameters().put('type','other');
           system.debug('value of the list in the executeSearchTest function++++++++++++'+pr);
           Test.setCurrentPage(pr);
           SearchFromURLController controller=new SearchFromURLController();
           system.assertEquals(false,controller.searched);
           //System.assertEquals(1, controller.performSearch().size());
           Test.StopTest();
    }
}

please help me to achieve the test functionality above 75% as of now it is 32%

  • value is a string property that is getting the search query from the page to the controller and then the function is calling and showing the results. – LALIT SINGH Feb 13 '15 at 9:38
  • You should take the advice of the below answers. Your code needs the parameter 'value' to be set in order to increase coverage. After all you are checking for that parameter in your controller and if it is null doing nothing. Thus the low coverage. – Eric Feb 13 '15 at 17:43
1

What you are missing in your test class is setting the URL parameter before testing. I am not 100% sure but I think this line is not effective way of putting parameters.

    PageReference pr=Page.SearchFromURL;
    pr.getParameters().put('name', 'Unit');
    pr.getParameters().put('industry', 'Energy');
    pr.getParameters().put('type', 'Hot');

Test.setCurrentPage(pr);

instead use this,

   Test.setCurrentPage(pr);
   ApexPages.currentPage().getParameters().put('name', 'Unit');
   ApexPages.currentPage().getParameters().put('industry', 'Energy');
   ApexPages.currentPage().getParameters().put('type', 'Hot');

You need to add this line before you call the Controller. Also you need to Test the EDIT method separately.

If this solves your problem please mark this as Solved.

1

The formatting in your question is a bit off, but it appears that your controller only does things if:

String nameStr=ApexPages.currentPage().getParameters().get('value');
if (null!=nameStr){ ... do stuff

Yet none of your test methods set a parameter called 'value', so it never does the stuff.

The best way to track this down is probably the Force.com IDE, also known as the Force.com Eclipse plugin. Tests may take longer to run, but in the end you'll get a detailed report including overall coverage from the tests you ran (all or some, your choice), and lists every line that isn't tested. Then you can modify your test methods or add to them accordingly, and re-run with a click.

Adding a bit more detail:

The test class sets a lot of parameters:

       pr.getParameters().put('name','Bernard');
       pr.getParameters().put('industry','Energy');
       pr.getParameters().put('rating','hot');
       pr.getParameters().put('type','other');

These are all meaninless to the controller you've posted - the controller never gets name, industry, rating, or type.

The controller is only looking for 1 parameter, called "value" which isn't ever set in any test method. Naturally the controller won't do much when it operates off a parameter that you're not passing it.

Here, the executeSearch won't fire unless the controller gets an apex page parameter called 'value':

    String nameStr=ApexPages.currentPage().getParameters().get('**value**');
     if (null!=nameStr)
    {
        value=nameStr;
        executeSearch();
    }

in executeSearch, only 'value' is passed to performSearch:

    accounts = performSearch(value);

in performSearch, only 'value' is used to build the query:

    topass = topass+'(name LIKE \'%'+ value +'%\'OR industry LIKE \'%'+ value +'%\'OR rating LIKE \'%'+ value + '%\'OR type LIKE \'%'+ value + '%\')';

Following the logic of the controller, it gets a null value for value and so it never calls executeSearch().

What you've build are 2 test cases of what the controller should do if no search value is passed to it. Add some asserts, maybe some error messages passed back to the visualforce page and assert those as well, and add some test cases where you post the value parameter and you should be on your way.

  • Value is a string property that is getting the search query from the page to the controller and then the function is calling and showing the results. – LALIT SINGH Feb 13 '15 at 9:39
  • @LALITSINGH , your test method never sets 'value', which is what would pass it to your controller Your test sets 'name', 'industry', 'rating', and 'type', but your controller is looking for 'value'. Your code does this: pr.getParameters().put('name','Bernard'); But never: pr.getParameters().put('value','testValueThatControllerCanActOn'); – SecondJon Feb 14 '15 at 21:22
  • Updated my post with more detail than is reasonable for a comment. :) – SecondJon Feb 14 '15 at 21:33

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.