+ Start a Discussion
infowelders_nsinfowelders_ns 

Test Method for a SOQL query

I have a very simple web controller that I need to write test code for:

 

 

public class web_controller_testimonial_list {

public list<Testimonial__c> listResults = new list<Testimonial__c>();

public list<Testimonial__c> getTestimonials() {
listResults = [SELECT quote__c, contact_name__c, contact_title__c, account_name__c FROM Testimonial__c WHERE Status__c = 'Published' ORDER BY Quote_Date__c desc];
return listResults;
}

public list<Testimonial__c> fetchTestimonials() {
getTestimonials();
return null;
}

}

 

 

 And really, I suppose it could be scaled down to something a little more simple, for the sake of this post:

 

 

public class web_controller_testimonial_list {

public list<Testimonial__c> getTestimonials() {
listResults = [SELECT quote__c, contact_name__c, contact_title__c, account_name__c FROM Testimonial__c WHERE Status__c = 'Published' ORDER BY Quote_Date__c desc];
return listResults;
}

}

 

 ... actually, I don't even know where to start.  I've been learning as I go, and I'm stumped.  How would I write a test case for something like this?

Thank you,

 

~ Nick

 

Message Edited by infowelders_ns on 09-21-2009 02:28 PM
AkiTAkiT

I would look examples from the reference material on testing:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing.htm 

infowelders_nsinfowelders_ns

I've already been through all of the documentation... I haven't found it all that helpful, unfortunately.  The apex documentation tends to be very complex, and doesn't ramp things up with simple examples first.  ><

JonSimmonsJonSimmons

 

Unit tests need to account for alot of data that is related but you can't count on them being there without adding them first.  In your case you need to know what results you will get from your listResults query, so you need to put the data in there in your unit test.

 

So you need to create a testimonial__c record then do whatever it takes to cause your trigger to run, then test to see if your trigger did what it was supposed to do.  If it updates that testimonial record in some way, or if the testimonial is used to update other records etc... then you need to test for that change.

 

 

if it were a simple rollup your unit test might be something like this

 

create temp account

Create temp testimonial__c record, linking to parent account

 -assume that your trigger is onInsert of the testimonial__c object and data is then rolled up to the account

check the account to verify that the testimonial__c data has been updated in the temp account

 

 

I don't know what your code is supposed to do so I don't know if this helps, but good luck. :)

 

Jon