1

i've spent nearly 2 days looking up how to write a test class and so I thought i'd now ask (works great in staging)

I have a custom controller that pulls records from a custom object, and then displays them as a table:

Apex class name: ouController

Custom object name: ou__c


public class ouController{

public List<ou__c> o_uy{get;set;}

 public ouController() {

    o_uy= [SELECT
         Affected_Receive_Country__c,
         Affected_Send_Country__c,
         Detailed_Description__c,
         End_Date__c,
         Incident_Description_for_CS__c,
         Incident_Status__c,
         Incident_Type__c,
         Service__c,
         Start_Date__c,
         Latest_Update__c,
         Customers_Notified__c,
         API_Enabled_Disabled__c,
         Action__c

             FROM ou__c
                 WHERE Incident_Status__c = 'Open'
                     ORDER BY Incident_Type__c DESC];
     }

  }

The VF page is this:

VF page name: o_u


 <apex:page Controller="ouController">

 <style>
 .headerclass{
 text-align:center;
 }
 </style>

<apex:pageBlock >           

    <apex:pageBlockTable value="{!o_uy}" var="ou__c" headerClass="headerclass" style="font-size:10.0pt;">>
        <apex:column value="{!ou__c.Start_Date__c}" style="{!IF(CONTAINS(ou__c.Incident_Type__c, 'Outage'), 'background-color: #FFC2C2;',IF(CONTAINS(ou__c.Incident_Type__c, 'Maintenance'), 'background-color: #9CDEFF;', 'background-color: #B0FF9C;'))}"/>
        <apex:column value="{!ou__c.End_Date__c}" style="{!IF(CONTAINS(ou__c.Incident_Type__c, 'Outage'), 'background-color: #FFC2C2;',IF(CONTAINS(ou__c.Incident_Type__c, 'Maintenance'), 'background-color: #9CDEFF;', 'background-color: #B0FF9C;'))}"/>
        <apex:column value="{!ou__c.Service__c}" style="{!IF(CONTAINS(ou__c.Incident_Type__c, 'Outage'), 'background-color: #FFC2C2;',IF(CONTAINS(ou__c.Incident_Type__c, 'Maintenance'), 'background-color: #9CDEFF;', 'background-color: #B0FF9C;'))}"/>
        <apex:column value="{!ou__c.Incident_Type__c}" style="{!IF(CONTAINS(ou__c.Incident_Type__c, 'Outage'), 'background-color: #FFC2C2;',IF(CONTAINS(ou__c.Incident_Type__c, 'Maintenance'), 'background-color: #9CDEFF;', 'background-color: #B0FF9C;'))}"/>
        <apex:column value="{!ou__c.Affected_Send_Country__c}" style="{!IF(CONTAINS(ou__c.Incident_Type__c, 'Outage'), 'background-color: #FFC2C2;',IF(CONTAINS(ou__c.Incident_Type__c, 'Maintenance'), 'background-color: #9CDEFF;', 'background-color: #B0FF9C;'))}"/>
        <apex:column value="{!ou__c.Affected_Receive_Country__c}" style="{!IF(CONTAINS(ou__c.Incident_Type__c, 'Outage'), 'background-color: #FFC2C2;',IF(CONTAINS(ou__c.Incident_Type__c, 'Maintenance'), 'background-color: #9CDEFF;', 'background-color: #B0FF9C;'))}"/>
        <apex:column value="{!ou__c.Incident_Description_for_CS__c}" style="{!IF(CONTAINS(ou__c.Incident_Type__c, 'Outage'), 'background-color: #FFC2C2;',IF(CONTAINS(ou__c.Incident_Type__c, 'Maintenance'), 'background-color: #9CDEFF;', 'background-color: #B0FF9C;'))}"/>  
        <apex:column value="{!ou__c.API_Enabled_Disabled__c}" style="{!IF(CONTAINS(ou__c.Incident_Type__c, 'Outage'), 'background-color: #FFC2C2;',IF(CONTAINS(ou__c.Incident_Type__c, 'Maintenance'), 'background-color: #9CDEFF;', 'background-color: #B0FF9C;'))}"/>            
        <apex:column value="{!ou__c.Customers_Notified__c}" style="{!IF(CONTAINS(ou__c.Incident_Type__c, 'Outage'), 'background-color: #FFC2C2;',IF(CONTAINS(ou__c.Incident_Type__c, 'Maintenance'), 'background-color: #9CDEFF;', 'background-color: #B0FF9C;'))}"/>            
        <apex:column value="{!ou__c.Latest_Update__c}" style="{!IF(CONTAINS(ou__c.Incident_Type__c, 'Outage'), 'background-color: #FFC2C2;',IF(CONTAINS(ou__c.Incident_Type__c, 'Maintenance'), 'background-color: #9CDEFF;', 'background-color: #B0FF9C;'))}"/>            
        <apex:column value="{!ou__c.Action__c}" style="{!IF(CONTAINS(ou__c.Incident_Type__c, 'Outage'), 'background-color: #FFC2C2;',IF(CONTAINS(ou__c.Incident_Type__c, 'Maintenance'), 'background-color: #9CDEFF;', 'background-color: #B0FF9C;'))}"/>            

    </apex:pageBlockTable>         
</apex:pageBlock>    


I'm really struggling here, i've gone through forums, Trailhead and the documentation. I understand that the purpose of the class is see what happens when a record from the custom object is passed through the controller

Conceptually, I believe it should be something like:


@isTest 
public class TestouController {
static testMethod void insertou() {

   ou__c testou__c = new ou__c();

   ou__c.Start_Date__c= DateTime.now();  //date/time field
   ou__c.End_Date__c= DateTime.now();  //date/time field
   ou__c.Service__c= 'Airtime';  //multi picklist field
   ou__c.Incident_Type__c= 'Maintenance';  //picklist field
   ou__c.Affected_Send_Country__c= 'Austria';  //picklist field
   ou__c.Affected_Receive_Country__c= 'Algeria';  //picklist field
   ou__c.Incident_Description_for_CS__c= 'test';  //text area 255 field
   ou__c.API_Enabled_Disabled__c= 'Enabled';  //picklist field
   ou__c.Customers_Notified__c= 'Email';  //multi picklist field
   ou__c.Latest_Update__c= 'test';  //Text(255) field
   ou__c.Action__c= 'test';  //text(50) field

   insert testou__c;
}
} 

However this is not working at all

1
  • The error i'm receiving is "Error: Compile Error: A value cannot be stored to Service__c in type ou__c at line 9 column 29 Sep 12, 2017 at 11:22

1 Answer 1

6

That's a good start. You're inserting one record in your test class.

The problem is, you don't even call your controller in your test, so it can't be test. Your code is never executed.

So after having inserted record, you just have to call your controller.

Finally, you have to make some assertions to verify that your controller query returns your inserted record.

Here's the right test code:

@isTest 
public class TestouController {
static testMethod void insertou() {

   ou__c testOu = new ou__c();

   testOu.Start_Date__c= '11/09/2017';  //date/time field
   testOu.End_Date__c= '11/09/2017';  //date/time field
   testOu.Service__c= 'Airtime';  //multi picklist field
   testOu.Incident_Type__c= 'Maintenance';  //picklist field
   testOu.Affected_Send_Country__c= 'Austria';  //picklist field
   testOu.Affected_Receive_Country__c= 'Algeria';  //picklist field
   testOu.Incident_Description_for_CS__c= 'test';  //text area 255 field
   testOu.API_Enabled_Disabled__c= 'Enabled';  //picklist field
   testOu.Customers_Notified__c= 'Email';  //multi picklist field
   testOu.Latest_Update__c= 'test';  //Text(255) field
   testOu.Action__c= 'test';  //text(50) field
   testOu.Incident_Status__c = 'Open';

   insert testOu;

   ouController controller = new ouController(); //here you call your code

   System.assertEquals(testOu.Id, controller.o_uy[0].Id); // here you verify that your controller variable is instantiated with the right value, ie your previously inserted record
}
} 
8
  • Thanks for the useful answer, i've amended my code, and i'm getting a compile error. This was first due to the date values I was inserting, my fault. I changed to: ou__c.Start_Date__c= DateTime.now(); But now i'm getting a error "Error: Compile Error: A value cannot be stored to Start_Date__c in type ou__c Sep 12, 2017 at 10:27
  • What's the type of your Start_Date__c field? Sep 12, 2017 at 11:40
  • it's a date/time field. I did some other testing, and this is actually affecting all of the fields that i'm testing. I looked at the custom object and i'm allowing API sharing and the fields are editable for all users Sep 12, 2017 at 11:49
  • To be clearer, Field Level Accessibility is Editable for the profiles that will be using the object Sep 12, 2017 at 11:52
  • Are you sure your question's code is the one you use? Sep 12, 2017 at 11:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .