You need to sign in to do that
Don't have an account?

Test class doesn't work after change to button action
I changed some code to act based on a button click rather than through JavaScript. The VF and Apex Class work very well. However, when I went to test it...
First, let me admit that I am flailing in the dark when it comes to testing. Someone else wrote the test code and it worked. I kind of understand it but not that well.
Here's the test class:
@isTest private class TEST_ContributionsSearch_v9 { static testMethod void testContributionSearchControllerEmpty () { //instantiate a page PageReference pg = Page.Contributions_Search_v9; Test.setCurrentPage(pg); pg.getParameters().put('contributorFname', ''); pg.getParameters().put('contributorLname', ''); pg.getParameters().put('MOC_Lname', ''); pg.getParameters().put('soqlLimitPage', ''); // instantiate the controller ContributionsSearchController_v9 controller=new ContributionsSearchController_v9(); System.assert(controller.runQuery() == null); System.assert(controller.runPicklists() == null); } static testMethod void testContributionSearchController () { //instantiate a page PageReference pg = Page.Contributions_Search_v9; Test.setCurrentPage(pg); pg.getParameters().put('contributorFname', 'a'); pg.getParameters().put('contributorLname', 'z'); pg.getParameters().put('MOC_Lname', 'g'); pg.getParameters().put('soqlLimitPage', 'limit 20'); // instantiate the controller ContributionsSearchController_v9 controller=new ContributionsSearchController_v9(); System.assert(controller.runSearch() == null); } }
Before the change contributorFname etc. were all passed to the controller from the VF by means of JS and <apex:actionFunction...
Now, contributorFname etc.are public strings in the controller with getters and setters.
I get the message: "System.NullPointerException: Attempt to de-reference a null object"
Stack Trace: "Class.ContributionsSearchController_v9.runSearch: line 94, column 1 Class.TEST_ContributionsSearch_v9.testContributionSearchController: line 30, column 1"
Line 30 is the second assert: "System.assert(controller.runSearch() == null);"
Line 94 references the variable contributorFname in this statement:
if (!contributorFname.equals(''))
soql_C += ' and Contributor__r.firstname LIKE \''+String.escapeSingleQuotes(contributorFname )+'%\'';
The first section of the test runs fine. I suspect that because the button doesn't get "clicked" the variables aren't set.
I'm happy to read doc on this but can't seem to find it. Links are welcome.
The controller and VF page follow my thanks.
THANKS... Bob
The Controller:
/* v7 fix repopulation search criteria issues */ /* v8 Clean out the remains of the javaScript call */ /* v9 Add other parms, create production candidate */ public with sharing class ContributionsSearchController_v9{ // the soql without the order and limit private String soql_C {get;set;} // the collection of contributions to display public List<Contributions__c> contributions {get;set;} // This is the base soql for building the query public String soqlBase_C { get{ return soqlBase_C ; } set; } // format the runReport URL for display on the visualforce page public String runReport{ get{ return 'https://cs10.salesforce.com/' ; } set; } // Create a query variable for debugging public String theQuery{ get {return theQuery; } set; } // Create a variable for record returned count Public Integer numberContributions{ get ; set; } // Create a STRING variable for record returned count Public string numberContributionsStr{ get{ return numberContributionsStr; } set; } // a set of variables to persist the search parameters Public string contributorFname{ get {return contributorFname; } set ; } Public string contributorLname{ get {return contributorLname; } set; } Public string MOC_Lname{ get {return MOC_Lname; } set; } Public string soqlLimitPage{ get {return soqlLimitPage; } set; } // END OF VARIABLE Declarations // init the controller and display some sample data when the page loads CONTRUCTOR public ContributionsSearchController_v9() { system.debug('v9.0'); system.debug('NO report button'); soqlBase_C = 'select ID, name, Contribution_Amount__c, Contribution_Date__c, Contribution_ID__c, Contribution_Type__c, Contributor__c, Contributor__r.firstname, Contributor__r.lastname, Contributor_City__c, Contributor_FEC_ID__c, Contributor_State__c, Contributor_Zip__c, Election_Cycle__c, MOC__c, MOC__r.firstname, MOC__r.lastname from Contributions__c where ID !=null '; system.debug(soqlBase_C); soql_C = soqlBase_C; runQuery(); } // end CONTRUCTOR // runs the actual query public void runQuery() { try { // Get Contribution Records if(soqlLimitPage == null) { soqlLimitPage = 'limit 20'; system.debug('TRY set null soqlLimitPage = ' + soqlLimitPage ); } theQuery = soql_C + ' ' + soqlLimitPage; system.debug(' theQuery = ' + theQuery); contributions = Database.query(theQuery); system.debug('Contributions list returned = ' + contributions); numberContributions = contributions.size(); numberContributionsStr = String.valueOf(numberContributions ); } // end try catch (Exception e) { ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, string.valueof(e))); ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!')); ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, soqlLimitPage)); ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, theQuery)); } // end catch } // end runQuery // runs the search with parameters passed via variables public PageReference runSearch() { // Display search variables system.debug('contributorFname = ' + contributorFname ); system.debug('contributorLname = ' + contributorLname ); system.debug('MOC_Lname = ' + MOC_LName); system.debug('soqlLimitPage= ' + soqlLimitPage); // Build the rest of the soql_ by tacking on and operator parameter soql_C = soqlBase_C; if (!contributorFname.equals('')) soql_C += ' and Contributor__r.firstname LIKE \''+String.escapeSingleQuotes(contributorFname )+'%\''; if (!contributorLname.equals('')) soql_C += ' and Contributor__r.lastname LIKE \''+String.escapeSingleQuotes(contributorLname )+'%\''; if (!MOC_Lname.equals('')) soql_C += ' and MOC__r.lastname LIKE \''+String.escapeSingleQuotes(MOC_Lname )+'%\''; // MOC__r.firstname, // now, go run the query again and exit runQuery(); return null; } // end runSearch // tests the variables, only function is testing Public void runPicklists() { List<String> junkList; String junkString; junkString = soqlLimitPage; } // end runPicklists } // end class
The VisualForce Page:
<apex:page controller="ContributionsSearchController_v9" sidebar="false"> <apex:form > <apex:pageMessages id="errors" /> <apex:pageBlock title="Contributions Search v9" mode="edit"> <table width="100%" border="0"> <tr> <td width="200" valign="top"> <apex:pageBlock title="Search Criteria" mode="edit" id="criteria"> <table cellpadding="2" cellspacing="2"> <tr> <td style="font-weight:bold;">Contributor First Name<br/> <apex:inputText id="contributorFname" value="{!contributorFname}"/> </td> </tr> <tr> <td style="font-weight:bold;">Contributor Last Name<br/> <apex:inputText id="contributorLname" value="{!contributorLname}"/> </td> </tr> <tr> <td style="font-weight:bold;">MOC Last Name<br/> <apex:inputText id="MOC_Lname" value="{!MOC_Lname}"/> </td> </tr> <tr> <td style="font-weight: bold;">Record Limit<br/> <apex:selectlist value="{!soqlLimitPage}" multiselect="false" size="1"> <apex:selectOption itemValue="limit 20" itemLabel="20 Records"/> <apex:selectOption itemValue="limit 40" itemLabel="40 Records"/> <apex:selectOption itemValue="limit 80" itemLabel="80 Records"/> </apex:selectlist> </td> </tr> <tr> <td style="font-weight: bold;"><br/> <apex:commandButton action="{!runSearch}" value="Run Search" rerender="results,debug" id="theSearch"/> </td> </tr> </table> </apex:pageBlock> </td> <td valign="top"> <apex:pageBlock mode="edit" id="results"> <apex:pageBlockTable value="{!contributions}" var="contribution"> <apex:column headerValue="Name" > <apex:outputField value="{!contribution.name}"/> </apex:column> <apex:column headerValue="Amount" > <apex:outputField value="{!contribution.Contribution_Amount__c}"/> </apex:column> <apex:column headerValue="Date" > <apex:outputField value="{!contribution.Contribution_Date__c}"/> </apex:column> <apex:column headerValue="Contributor First Name" > <apex:outputField value="{!contribution.Contributor__r.firstname}"/> </apex:column> <apex:column headerValue="Contributor Last Name" > <apex:outputField value="{!contribution.Contributor__r.lastname}"/> </apex:column> <apex:column headerValue="City" > <apex:outputField value="{!contribution.Contributor_City__c}"/> </apex:column> <apex:column headerValue="State" > <apex:outputField value="{!contribution.Contributor_State__c}"/> </apex:column> <apex:column headerValue="Zip" > <apex:outputField value="{!contribution.Contributor_Zip__c}"/> </apex:column> <apex:column headerValue="Cycle" > <apex:outputField value="{!contribution.Election_Cycle__c}"/> </apex:column> <apex:column headerValue="MOC First Name" > <apex:outputField value="{!contribution.MOC__r.firstname}"/> </apex:column> <apex:column headerValue="MOC Last Name" > <apex:outputField value="{!contribution.MOC__r.lastname}"/> </apex:column> </apex:pageBlockTable> <br/> <br/> <apex:outputText style="font-weight: bold;" value=" Note: {!numberContributionsStr} Contribution Records match your criteria. " /> <br/> <br/> <apex:outputText value=" To see more records, change the Record Limit criteria above IF {!numberContributionsStr} was the limit. " /> <br/> <br/> <apex:outputText value="Contributions Search v9" /> <br/> <br/> </apex:pageBlock> </td> </tr> </table> <apex:pageBlock title="Debug - SOQL - theQuery" id="debug"> <apex:outputText value="{!theQuery}" /> <br/> <apex:outputText value="Records Returned: {!numberContributionsStr}" /> <br/> </apex:pageBlock> </apex:pageBlock> </apex:form> </apex:page>