function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
angusgrantangusgrant 

Test Method not returning querystring values

Hi I have this class below that I am trying to write a test method for:

 

// Decare Vars from Querystring
String accountrecordtypeid = ApexPages.currentPage().getParameters().get('AccountRecordTypeID');
String contactrecordtypeid = ApexPages.currentPage().getParameters().get('ContactTypeID');
String eventid = ApexPages.currentPage().getParameters().get('eventID');

/* Retrieve current Event name to be displayed in application form.
We also need to handle the circumstances when no event is returned in which case a */
public SBS_Event__c SelectedEvent{
get{
try {
sbs_event = [select Name from SBS_Event__c where ID = :eventid and Display_on_website__c = True and Event_Start_Date__c > Today limit 1];
}
catch (DmlException de){
System.debug('Data exception:'+ de.getMessage());
return null;
}
catch (Exception e) {
System.debug('General exception:'+ e.getMessage());
}
return sbs_event;
}

private set;}

 

 For some reason I cant return any rows from the SOQL marked in red above.

 

This is my test method in which I am passing in valid querystring paramters

 

//select random events to test Note there Must be an event in the system ApexPages.currentPage().getParameters().put('AccountRecordTypeID',[Select ID from RecordType Where SobjectType = 'Account' Limit 1].id); ApexPages.currentPage().getParameters().put('ContactTypeID',[Select ID from RecordType Where SobjectType = 'Contact' Limit 1].id); ApexPages.currentPage().getParameters().put('eventID',[select id from SBS_Event__c Where Display_on_website__c = True and Event_Start_Date__c > Today Limit 1].id); SBS_Event__c SBSEvent = extension.SelectedEvent; extension.picklistValue = '0';

 

 

 

 

see error log below:

 

 

20090613120908.297:Class.appExtensionTestClass.testappExtension: line 15, column 74: SOQL query with 1 row finished in 5 ms
20090613120908.297:Class.appExtensionTestClass.testappExtension: line 16, column 68: SOQL query with 1 row finished in 2 ms
20090613120908.297:Class.appExtensionTestClass.testappExtension: line 17, column 62: SOQL query with 1 row finished in 4 ms
20090613120908.297:Class.appExtension: line 38, column 18: SOQL query with 0 rows finished in 2 ms
20090613120908.297:Class.appExtension: line 45, column 4: General exception:List has no rows for assignment to SObject

 

If I cut and paste the IDs passed through the page to the actual form it works perfectly.

Any ideas?

Best Answer chosen by Admin (Salesforce Developers) 
David VPDavid VP

In your test try to put your parameters first and then initialize your extension.

It's hard to see from the snippets but it looks like in your test you're first initializing the page and then setting your querystring parameters (but then it's too late).

 

David

All Answers

greenstorkgreenstork

Here's my guess, and it is just an educated guess, but it looks like you're trying to cast a String (eventid) as an Id.  Try making the variable an Id instead of a string and see if that helps.

 

Edit: Either it's the problem I mentioned above or the reverse, that is, you're not allowed to put an Id in a querystring without casting it as a String first. The solution there would be to create the variable, run your SOQL's to set your string variables and then throw those string variables intoyour querystring in your tests. I don't know for sure if this is the problem, but that's where I would look first.

Message Edited by greenstork on 06-14-2009 07:04 PM
angusgrantangusgrant

Thanks for getting back to me.

 

I tried setting the variables in my controller extension from strings to Id's but too no avail the page still worked but my test method did not return any rows. I also tried explicitly defining the querystring parameter to be put into the test method.

 

But again this made no difference any input appreciated and no rows were returned.

 

 

public class appExtensionTestClass { public static testMethod void testappExtension() { // Testing the Application form Controller ApexPages.standardController controller = new ApexPages.standardController(new Contact()); appExtension extension = new appExtension(controller); //test Event Listing before paramaters are populated should get into try catch block. ApexPages.currentPage().getParameters().put('eventID','a018000000LtwAMAAZ'); SBS_Event__c SBSEvent = extension.SelectedEvent;

 

 20090615151757.325:Class.appExtensionTestClass.testappExtension: line 7, column 30: returning from end of method public appExtension(ApexPages.StandardController) in 2 ms
20090615151757.325:Class.appExtension: line 38, column 18: SOQL query with 0 rows finished in 7 ms
20090615151757.325:Class.appExtension: line 45, column 4: General exception:List has no rows for assignment to SObject
20090615151757.325:Class.appExtensionTestClass.testappExtension: line 11, column 29: returning SOBJECT:SBS_Event__c from method public SOBJECT:SBS_Event__c in 7 ms
20090615151757.325:Class.appExtensionTestClass: line 3, column 31: returning from end of method public static testMethod void testappExtension() in 9 ms

 

 

 

 

 

David VPDavid VP

In your test try to put your parameters first and then initialize your extension.

It's hard to see from the snippets but it looks like in your test you're first initializing the page and then setting your querystring parameters (but then it's too late).

 

David

This was selected as the best answer
angusgrantangusgrant

Hay David thanks for your help with this. I got the test coverage from 21% to 57%. Just got to work out how to pass through the form fields from the standard contact controller into my save method in the controller extension.

 

This is the method I tried:

 

Contact contact = new Contact(
Salutation = 'Dr.',
Firstname = 'testier',
Lastname = 'tester',
Email = 'test@test.com',
Phone = '07875 123323',
MailingStreet = 'Mailing Street',
MailingCity = 'Mailing City',
MailingState = 'Mailing State',
MailingPostalCode = 'OX1 XXX',
MailingCountry = 'Timbucktoo',
Job_Title__c = 'Job Title');

This first attempt does not pass the field values into the save method and I get msg FIELD_CUSTOM_VALIDATION_EXCEPTION, First Name must be more than one Character: [FirstName] which is a custom validation rule I settup.

 

 

busextension = new appExtension(controller);
busextension = Firstname('test.');
busextension = Lastname('testier');
busextension = Email('test@test.com');

 

 

 The 2nd Method does not compile:

 

error msg: Method does not exist incorrect signitureon highlighted line
David VPDavid VP

Hi,

 

Sorry I didn't get back to you earlier .. I'm taking some time of in a place where internet access isn't so obvious.

Try opening a new thread with this specific problem and make sure to post your extension class code + your test code.

Basically : you need to call the setters yourself if you want to simulate the VF page filling in variables.

I hope others can chime in to help you. If not : just wait for me to get back in a week or so and I'll help you out.

 

Happy coding !!

 

David