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
KlivingstonKlivingston 

Test Class

Hi everyone,

Can you please help me to write a test class for below code:

 

public class retrieveOpenSiteDefects    {     
      private ApexPages.StandardSetController controller;                                   
      public List<Site_Defects__c> getOutbaseSiteDefects()                
       {                          
             return [SELECT Id, Name, Date_Raised__c,Contractor__c,Defect_Type__c,Status__c         
             FROM Site_Defects__c WHERE Status__c = 'Open' order by Date_Raised__c desc limit 10];           
        }
}

 I have been trying by this:

 

@isTest
public class testRetrieveOpenIncidents{

    static testMethod void testOpenIncident(){
        Incident__c i = new Incident__c(Name='test',Current_Owner__c= 'me',  Date_Occurred__c= 2013-11-12,
                                        Description__c= 'none', Due_Date__c= 2013-11-25,
                                        Open_Actions__c= 'Investigate,',
                                        RCA_Approved__c='Y',
                                        RCA_Submission_Date__c= 2013-11-12,
                                        Status__c= 'Under Investigation',
                                        Terminal__c= 'Test'
                                        Transport_Contractor__c= 'salesforce test'
                                        )
    
    
    
    }

 

for some reason it does not like date :/

 

Thanks

 

 

Stephanie_ArceStephanie_Arce

I'm not sure if this is the best way to do it (I'm new to Apex and coding in general), but I've found this works: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_System_Date_newInstance.htm

 

For instance:

Date.NewInstance(system.today().year(), system.today().month(), 15);

 will return the 15th of the current month.

KlivingstonKlivingston

 

@isTest
public class testRetrieveOpenIncidents{

    static testMethod void testOpenIncident(){
        
        Incident__c in1 = new Incident__c(Name='new incident',Current_Owner__c= 'me',  Date_Occurred__c= Date.NewInstance(system.today().year(), system.today().month(), 13),
                                        Description__c= 'none', Due_Date__c=  Date.NewInstance(system.today().year(), system.today().month(), 13),
                                        Open_Actions__c= 'Investigate,',
                                        RCA_Approved__c='Y',
                                        RCA_Submission_Date__c=  Date.NewInstance(system.today().year(), system.today().month(), 15),
                                        Status__c= 'Under Investigation',
                                        Terminal__c= 'a0Ag0000002JU9L',
                                        Transport_Contractor__c= 'a0fg0000000u09l');
    
    insert in1;
    
    }

}

 

Abhi_TripathiAbhi_Tripathi

Hey 

KlivingstonKlivingston

Hi Abhi

 

can you please help me to complete this test class. I checked your link but I could not figure out how it works. My custom class is called by a VisualForce page. And I wrote this test class as its need %75 code coverage to be pushed to production.

 

How to create an instance of my custom class in the test class?

 

Thanks

 

KlivingstonKlivingston

This is my VF page:

<apex:page controller="retrieveOpenProcedureObservations" tabStyle="Report" >

    <apex:pageBlock >
      <apex:pageBlockTable value="{!OutbaseProcedureObservations}" var="c">
            <apex:column title="Site Number">
                <apex:outputLink value="/{!c.id}" target="_blank">{!c.Name}</apex:outputLink>
            </apex:column>      
             <apex:column value="{!c.Title__c}" onclick="window.open('https://salesforce.com/{!c.id}','_blank')"/>
            
            <apex:column value="{!c.Contractor__c}"/>
            <apex:column value="{!c.Category__c}"/>
            <apex:column value="{!c.Status__c}"/>  
            <apex:column value="{!c.Observation_Date__c}"/>  
            
            
                  
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>