+ Start a Discussion
Gabriel McGinnGabriel McGinn 

Creating an Apex Test Class

Most of the resources I've found online reference specifically creating records not updating existing records only. 
Any assistance would be appreciated.

The following Apex class is staged in my sandbox. It is used in conjunction with a visualforce page and is launched via a button on the Incident object.

Here is the class:
public class IncidentTask
{
    public ApexPages.StandardController std;
 
    // The associated tasks to the Parent Incident
    public List<BMCServiceDesk__Task__c> tasks {get; set;}
    
    public PageReference save()
    {

     // first save the incident
        std.save();
       
     // then save the procurement tasks
        update tasks;

     // set current page as reference to be returned to after save commits
        PageReference result=ApexPages.currentPage();
         result.setRedirect(true);       
        
        // navigates to page set above with updated values
        return result;
    }

    //
    public String[] records {get;set;}
    
    public IncidentTask(ApexPages.StandardController stdCtrl)
    {
     std=stdCtrl;
     // queries related fields for the task object so they can be referenced in the visualforce page.
     tasks=[select id, Name, Subject__c, Model__c, CMDB_Model__c, BMCServiceDesk__FKIncident__c, BMCServiceDesk__FKStatus__c, Tracking_Number__c, Asset_Tag__c, Serial_Number__c, Cost__c, Vendor_Name__c from BMCServiceDesk__Task__c where BMCServiceDesk__FKIncident__c=:std.getId() order by Name asc limit 400];

    // dynamically create set of unique Line Items from query
    Set<String> taskSet = new Set<String>();
        for (BMCServiceDesk__Task__c t : tasks)
        taskSet.add(t.Subject__c);

    // convert the set into a string array  
    records = new String[taskSet.size()];
    Integer i = 0;
        for (String record : taskSet) { 
        records[i] = record;
        i++;
        }
    }

}
Raj VakatiRaj Vakati
@istest()
public class IncidentTaskTest
{
      private static testmethod void IncidentTaskTest()
     {


     BMCServiceDesk__Task__c obj = new BMCServiceDesk__Task__c(Name ='Test');
     insert obj;

	 
	 
         PageReference pageRef = Page.YOURPAGE;
         Test.setCurrentPage(pageRef);

     pageRef.getParameters().put('Id', String.valueOf(obj.Id));
     ApexPages.StandardController sc = new ApexPages.StandardController(obj);
     IncidentTask con = new IncidentTask(sc);
con.save();

         }
     }

 
Raj VakatiRaj Vakati
@istest
public class IncidentTaskTest
{
      private static testmethod void IncidentTaskTest()
     {


     BMCServiceDesk__Task__c obj = new BMCServiceDesk__Task__c(Name ='Test');
     insert obj;

	 
	 
         PageReference pageRef = Page.YOURPAGE;
         Test.setCurrentPage(pageRef);

     pageRef.getParameters().put('Id', String.valueOf(obj.Id));
     ApexPages.StandardController sc = new ApexPages.StandardController(obj);
     IncidentTask con = new IncidentTask(sc);
con.save();

         }
     }