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

Simple Test Class Help
I've just written my first custom controller. It's very basic and returns data from a custom object and sorts it by a date field.
The data is returned and then displayed on my visualforce page.
public List<News__c> newsList {get; set;} public ctr_News() { } // Get news method to return data public List<News__c> GetNews(){ // Creates new list of News // Below creates a new String based on the SOQL query, ordered by sort date String strSql = 'SELECT Id, Name, Preview_Text__c, NewsBody__c, CreatedDate, Sort_Date__c FROM News__c ORDER BY Sort_Date__c ASC'; return database.query(StrSql); // Returns the SOQL string } // End of method } // End of class
How would I go about putting a test class into this to verify the data returned is not null?
Hey Alanistic,
Go for this post , surely it will help you
http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html
Alanistic,
Your test class should look something like the following:
@istest
public class TestNewsList{
public static TestMethod Void TestNewsList(){
date testdate = date.today();
newslist NList= new newslist();
news__c n = new news__c(Name='Test News', Preview_Text__c = 'Preview Text', newsbody__c ='Test news body', createddate = testdate, sort_date__c = testdate);
insert n;
system.assert(name, 'Test News');
system.assert(Preview_text__c, 'Preview Text');
system.assert(newsbody__c,'Test news body');
system.assert(testdate, createddate);
system.assert(testdate, sort_date__c);
NList.getnews();
}
}
If this helped you, please mark this as the solution and don't forget to give Kudos!
Thanks Josh.
This shows an error:
Only top-level class methods can be declared static
This occurs at the line:
Is there something i need to change?
How are you running the test? Are you using the "Execute Anonymous window? If so, you will generate that error. Try running the class by going into setup>>Develop>>Apex Classes. Find the class and click on it. Then click "Run Test."
Don't forget to accept as solution and give Kudos!!!
Hi Josh, this is when saving the class. I've added it to the same code as my apex class. Should they be in seperate files? Is there a way to have the actual code and the test code all in the same file?
No, your test code should be in a separate class. You have a class to do some function. . . You have a test class to test the class that does that function.
Don't forget to accept as solution and give KUDOS!!!