Skip to main content
Gabriel Teles a posé une question dans #Apex
Hi everyone, I modified this sample (http://bobbuzzard.blogspot.com.br/2010/09/visualforce-lookup.html) and I tried to write the test class to put in production, but not worked.

Could anyone help me?

 

//My controller

public class FilterPopupController {

public String query {get; set;}

public List<Custom__c> vs {get; set;}

public PageReference runQuery(){

try{

List<List<Custom__c>> searchResults=[FIND :query IN ALL FIELDS RETURNING Custom__c (id, name)];

vs=searchResults[0];

return null;

}catch(Exception e) {

ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ops!!! '));

return null;

}

}

}

//My test class

@isTest

private class FilterPopupTest {

public static testMethod void FilterPopupTest() {

PageReference pageRef = Page.LookupPopup;

Test.setCurrentPage(pageRef);

Custom__c c = new Custom__c();

c.Name = 'Name';

insert v;

String query;

Id [] fixedSearchResults= new Id[1];

fixedSearchResults[0] = '001x0000003G89h';

Test.setFixedSearchResults(fixedSearchResults);

List<List<SObject>> searchList = [FIND 'test'

IN ALL FIELDS RETURNING Custom(id, name)];

}

}

 
2 réponses
  1. 27 oct. 2015, 02:24
    You're not testing the class at all, rather just running the same query again.  Since you're not testing the class, you won't have any code coverage for that class.  Instead, you need to construct your class (by calling the class's constructor) and then running the runQuery method.  See the example below:

     

    //My test class

    @isTest

    private class FilterPopupTest {

    public static testMethod void FilterPopupTest() {

    PageReference pageRef = Page.LookupPopup;

    Test.setCurrentPage(pageRef);

    Custom__c c = new Custom__c();

    c.Name = 'Name';

    insert v;

    Test.startTest();

    FilterPopupController ctrl = new FilterPopupController();

    //Set the query member variable...

    ctrl.query = 'Name';

    ctrl.runQuery();

    Test.stopTest();

    System.assertEquals(1,ctrl.vs.size());

    System.assertEquals(1,ctrl.vs.get(0).size());

    }

    }

    Note, you'll have to cover edge cases as well, such as when exceptions are thrown or no results returned.
0/9000