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
BrettEldridgeBrettEldridge 

Simple Test Class Help

Hello,

 

I am very new to Apex....    Been thrown in, and need to learn fast.

 

I have just started looking into test classes, and need some help.  I am designing a simple website that displays text from a record. Easy....   

 

My question is, how do you test something that doesn't really have anything to test?

 

Here is my class for the page:

 

public class Web_Property_Home {

 

private final Website_Page__c field;

public Web_Property_Home(){

field = [select name,id,Main_Heading__c,Main_Text__c  from Website_Page__c where Name = 'Home Page'];

 

public Website_Page__c getfield(){

return field; 

}


}

 

 

ANY Help would be appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
vishal@forcevishal@force

Hi,
As stForce rightly pointed out the need for system.asserts which will actually check if you are getting the ecpected values or not. 

 

What also is really important when writing test coverages is "test data" , ie test records for all the objects that you are referring in your class.

 

The reason behind this is :

 

In your class you are querying from Website_Page__c object, now what can happen is that in one of the Orgs, you have the record which satisfies the condition and returns a record but while deploying your class to any other org, it may happen that the destination org is not having any such record, there you will get an exception.

so to make sure this doesn't happen, we create test records.

 

for your query :[select name,id,Main_Heading__c,Main_Text__c  from Website_Page__c where Name = 'Home Page']

all you need to add is a Website_Page__c record where Name is 'Home Page'.

 

Website_Page__c objWeb = new Website_Page__c();

objWeb.Name = 'Home Page';insert objWeb;
Web_Property_Home objClass = new Web_Property_Home ();objClass.getfield();

Here before you are testing your class, you are creating a test record so that you will always have a record for your query.
Hope this clears your doubt.

All Answers

stcforcestcforce

Given an existing record, you just test that the field values are what you would expect.

Web_Property_Home x = new Web_Property_Home();

system.assert(x.field != null);//unless you have something more exact.

 

test what you can (the test coverage is actually based on how many lines of code are run, so the first line will get you your required coverage but the second is more to do with good practice). If you don't test, it will obviously bite you at some later point  - best to maintain a high level of coverage rather than constantly dipping below the level for deployment.

 

BrettEldridgeBrettEldridge

Thank you for the reply.  That worked, but only checked at 60%

 

 

 

Here is my entire class, just incase it helps. Also, I am really sorry for my percievable obvious questions, but I am php, and have never seen this stuff  :)

 

PS. All I want to do, I get the fields from a record and display them on a page. It is a cms for a website using salesforce.  Should be really easy  :)

 

Here it is, feel free to offer a totally different piece of code if I have made heaps of errors:

 

 

public class Web_Property_Home {

      private final Website_Page__c field;
        
      public Web_Property_Home() {
            field = [select name,id,Main_Heading__c,Main_Text__c,Text_Field_1__c,Text_Field_2__c,
            Text_Field_3__c,Sub_Heading_1__c,Sub_Heading_2__c,Sub_Heading_3__c,Image_1__c,Image_2__c from Website_Page__c where Name = 'Home Page'];
      }
      public Website_Page__c getfield() {
      
           return field;
      }
static testMethod void Web_Property_Home(){
Web_Property_Home x = new Web_Property_Home();
system.assert(x.field != null);//unless you have something more exact.
}
}

 

vishal@forcevishal@force

Hi,
As stForce rightly pointed out the need for system.asserts which will actually check if you are getting the ecpected values or not. 

 

What also is really important when writing test coverages is "test data" , ie test records for all the objects that you are referring in your class.

 

The reason behind this is :

 

In your class you are querying from Website_Page__c object, now what can happen is that in one of the Orgs, you have the record which satisfies the condition and returns a record but while deploying your class to any other org, it may happen that the destination org is not having any such record, there you will get an exception.

so to make sure this doesn't happen, we create test records.

 

for your query :[select name,id,Main_Heading__c,Main_Text__c  from Website_Page__c where Name = 'Home Page']

all you need to add is a Website_Page__c record where Name is 'Home Page'.

 

Website_Page__c objWeb = new Website_Page__c();

objWeb.Name = 'Home Page';insert objWeb;
Web_Property_Home objClass = new Web_Property_Home ();objClass.getfield();

Here before you are testing your class, you are creating a test record so that you will always have a record for your query.
Hope this clears your doubt.

This was selected as the best answer
BrettEldridgeBrettEldridge

Hi, I combined the above, and got 100%

 

static testMethod void Web_Property_Home(){

Website_Page__c objWeb = new Website_Page__c();
objWeb.Name = 'Home Page';insert objWeb;
Web_Property_Home objClass = new Web_Property_Home ();objClass.getfield();

system.assert(objClass.field != null);
}

 

Only thin was, I didn't have a limit of 1 on my database query, which was required. I added that, and got 100%.  Thanks guys.