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
CapCbRMCapCbRM 

Help with TestMethod

Hi DF,

 

I have completed a class to use as an extension of a VF page. I use it on a "Worldcolor_Project" page layout to show the Schedule associated with the project and the underlying Milestones attached to that Schedule.

 

The save and delete methods are used for VF buttons.

 

I am currently confused on how to begin my Testmethod. I started with Creating a new Worldcolor_Project and schedule related but am unsure how to test the inidividual methods. Any help would be greatly appreciated.

 

 

public with sharing class PullMilestones_Worldcolor {

    public Milestone__c[] milestones = new Milestone__c[0];
    public Schedule__c[] scheduleinfo;
   
    public PullMilestones_Worldcolor(ApexPages.StandardController controller) {
    }
    

    public Milestone__c[] getMilestones() {
        
         milestones = [SELECT ID, Name, Duration__c, Status__c, Due_Date__c,Start_Date__c,  Milestone_Status__c, Index__c, Predecessor_Index__c, Responsible__c
                        FROM Milestone__c
      WHERE Worldcolor_ProjectID__c = :ApexPages.currentPage().getParameters().get('id')
                            ORDER BY Index__c];
                            
        return milestones;
    }
    
    public void saveMile(){ UPDATE milestones; } 
       


    public Schedule__c[] getSchedule() {

        scheduleinfo = [SELECT ID, Name, Details__c, Start_Date__c, End_Date__c, Method__c, Duration__c, Overdue_Milestones__c, Risk_Status__c, Schedule_Template__c  
                            FROM Schedule__c 
                             WHERE WorldColor_Project__c = :ApexPages.currentPage().getParameters().get('id')];
        
        return scheduleinfo;
    }
	 
     public void saveSched() { UPDATE scheduleinfo; }     
     public void delSched() { DELETE scheduleinfo; }

}

 

 

CapCbRMCapCbRM

 

public static testMethod void testPullMilestones_Worldcolor() {
        // Instantiate a new controller with all parameters in the page
        PullMilestones_Worldcolor controller = new PullMilestones_Worldcolor();
        
        WorldColor_Project__c testProj = new WorldColor_Project__c(
        Name = 'Test1',
        Address__c = 'testStreet',
        City__c = 'testCity'
          );
			insert testProj;
			
		Schedule__c testSched = new Schedule__c(
		Name = 'Test2', 
		WorldColor_Project__c = testProj.Id
		  );
			insert testSched; 
	
	 					
	 ApexPages.currentPage().getParameters().put('id', testProj.id );
     
      testSched.Name = 'updatedname';
      controller.getMilestones();
      controller.getSchedule();
      
      	Schedule__c scheduleinfo = new Schedule__c(
		Name = 'scheduleinfo', 
		WorldColor_Project__c = testProj.Id
		  ); 
			insert scheduleinfo;
      
      controller.saveSched();
      controller.delSched(); 
	  
	 Milestone__c[] milestones = new Milestone__c[]{};
			insert milestones;
		
		controller.saveMile(); 
	  
    }

This got me to 95% so I can live with that :)