You need to sign in to do that
Don't have an account?
Error "unexpected token" when saving a test class on a controller extension
I'm attempting to write my first Test Class for a Controller extension on a custome object. Here is the extension code and the test class (in one file):
public class ItemEditController {
private ApexPages.StandardController controller {get; set;}
public Ad_Order__c[] searchResults {get;set;}
public string searchText {get;set;}
// standard controller - could also just use custom controller
public ItemEditController(ApexPages.StandardController controller) { }
// fired when the search button is clicked
public PageReference search() {
string searchFor = '%' + searchText + '%';
String qry = 'SELECT CreatedDate, CreatedById, Campaign_Name__c, Name, Flight_Start_Time__c, Flight_End_Time__c, Sales_Office__c,Status__c FROM Ad_Order__c' +
' where ((name LIKE :searchFor) OR (Campaign_Name__c LIKE :searchFor)) order by Status__c, name Limit 100';
searchResults = Database.query(qry);
return null;
}
public class Test_ItemEditController {
Ad_Order__c myOrder = new Ad_Order__c();
ApexPages.Standardcontroller con = new ApexPages.Standardcontroller(myOrder);
ItemEditController ext = new ItemEditController(con);
String mySearch= new String();
mySearch = 'Kathys';
ext.setSearchText(mySearch);
List<Account_Order__c> myList = new List<Account_Order__c>;
pageReference pr=new PageReference();
pr = ext.search();
myList=ext.searchResults;
if(myList.size <> 0){
System.debug('Search returned results');
}
}
}
However, I'm getting Save Error: unexpected token '=' on this line: mySearch = 'Kathys'; whenever I save it.
Can anyone see what I'm doing wrong?
I apologize if this is a dumb question, but I've searched all over and don't understand what is wrong with my code.
I really appreciate any help you can offer.
I am assuming you test_xxx is a test method? if so:
change to this
All Answers
Hi kathybb,
try changing the query to this
according to me, the colon can be used only when you are querying using the square brackets "[ ]"
Hope this helps,
Sid
Thanks, Sidz.
I tried your suggestion, but either way I still get the error "Unexpected token:'='"on the line that says mySearch = 'Kathys'; in the public class Test_ItemEditController { section of my code. I'm not understanding why this code is causing an error when I save, but it's preventing me being able to test my controller extension code.
Thanks in advance for any help.
Kathy,
You cannot construct a string like String xxxx = New String() as you have done here
String mySearch= new String();
mySearch = 'Kathys';
Simply change it to:
String mySearch = 'Kathys';
Note: I did not review the rest of the code so this may not completely resolve your issue. If it does not post back here any other issues that arise.
Starz26,
Thank you for your help.
Evidently there was more than one thing wrong with my code -- I made the changes per your instructions and now the line:
ext.setSearchText(mySearch); is making it throw Error unexpected token: ')'. Do you know what might be causing that?
I really appreciate your help.
Can you post your current code?
Sure, I'd be happy to. Here it is:
public class ItemEditController {
private ApexPages.StandardController controller {get; set;}
public Ad_Order__c[] searchResults {get;set;}
public string searchText {get;set;}
// standard controller - could also just use custom controller
public ItemEditController(ApexPages.StandardController controller) { }
// fired when the search button is clicked
public PageReference search() {
// string searchFor = '%' + searchText + '%';
//String qry = 'SELECT CreatedDate, CreatedById, Campaign_Name__c, Name, Flight_Start_Time__c, Flight_End_Time__c, Sales_Office__c,Status__c FROM Ad_Order__c' +
// ' where ((name LIKE :searchFor) OR (Campaign_Name__c LIKE :searchFor)) order by Status__c, name Limit 100';
string searchFor = '%' + 'kathys'+ '%';
String qry = 'SELECT CreatedDate, CreatedById, Campaign_Name__c, Name, Flight_Start_Time__c, Flight_End_Time__c, Sales_Office__c,Status__c FROM Ad_Order__c' + ' where ((name LIKE \''+searchFor+'\') OR (Campaign_Name__c LIKE \''+searchFor+'\')) order by Status__c, name Limit 100'; System.debug(qry);
searchResults = Database.query(qry);
return null;
}
public class Test_ItemEditController {
Ad_Order__c myOrder = new Ad_Order__c();
ApexPages.Standardcontroller con = new ApexPages.Standardcontroller(myOrder);
ItemEditController ext = new ItemEditController(con);
String mySearch = 'Kathys';
ext.setSearchText(mySearch);
List<Account_Order__c> myList = new List<Account_Order__c>;
pageReference pr=new PageReference();
pr = ext.search();
myList=ext.searchResults;
if(myList.size <> 0){
System.debug('Search returned results');
}
}
}
The controller takes a search term from a box on the page, and searches for that text in either the name field called order_ number__c field (which is an autonumber text field) or the campaign_name__c field. The controller appeared to work correctly from the UI side.
I really appreciate your help with this -- I'm the only salesforce/visualforce/apex developer, so I am pretty much teaching my self this stuff.
You do not have a set method for your variable, you are using the default setter which is fine.
Change
ext.setSearchText(mySearch);
to
ext.searchText = mySearch;
I changed the line as you suggested, but when I save, I'm still getting
Save error: unexpected token: '=' ItemEditController.cls /AdPortal Sandbox/src/classes line 25 Force.com save problem when I save. I really appreciate your help and time on this.
I am assuming you test_xxx is a test method? if so:
change to this