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
Bryan TelfordBryan Telford 

Code coverage for Apex class with page reference method

I have the following class which fails to get code coverage on lines in bold. I have searched through the forums and can't determine how to get those particular lines covered. 

public without sharing class projectWizardController {

Project__c project;

   public Project__c getProject() {
      if(project == null) project = new Project__c();
      project.Plan__c = ApexPages.currentPage().getParameters().get('planId');
      project.Status__c = 'New';
      return project;
   }    
   
   public PageReference step1() {      
      return Page.new_project_p1;
   }
   
   public PageReference step2() {      
      return Page.new_project_p2;
   }
   
   public PageReference step3() {      
      return Page.new_project_p3;
   }
             
   public PageReference submit() {

      try {     
          //if(project.get('Id') == null) 
          project.Status__c = 'Submitted';
          project.Submitted__c = Date.Today();
          insert project;
          PageReference projectPage = new PageReference('/apex/manage_project?id=' + project.id + '&planId='+          ApexPages.currentPage().getParameters().get('planId'));
          projectPage.setRedirect(true);         
          return projectPage;

    
        }
      catch (Exception e) {apexpages.addmessages(e);}return null;

     }         
}

The test class is below.
@isTest
public class projectWizardControllerTest {
  private static testmethod void testSubmittedProject() {
 
    projectWizardController controller = new projectWizardController(); 

    //ARRANGE    
    Plan__c plan = new Plan__c();insert plan;
    Project__c project = controller.getProject();
    project.Plan__c = plan.id;
    project.Status__c = 'Submitted';
    insert project;
    
    //ACT
    Test.startTest();
        controller.step1();
        controller.step2();
        controller.step3();                
        controller.submit();           
        PageReference pageRef = Page.manage_project;
        Test.setCurrentPage(pageRef);
        ApexPages.currentPage().getParameters().put('planid',plan.id);
        ApexPages.currentPage().getParameters().put('id',project.id);
        ApexPages.currentPage().setRedirect(true);        
    Test.stopTest();        
    //ASSERT
    System.assertEquals(project.Status__c, 'Submitted');         
}
}
Best Answer chosen by Bryan Telford
Nayana KNayana K
@isTest
public class projectWizardControllerTest {
  private static testmethod void testSubmittedProjectSuccess() {
	/*Prepare Test Data */
	Plan__c plan = new Plan__c(/*POPULATE ANY REQUIRED FIELD VALUES HERE*/);
	insert plan;
	// pass plan id for the new project creation
	ApexPages.currentPage().getParameters().put('planid',plan.id);
	Test.setCurrentPage(Page.manage_project);
    projectWizardController controller = new projectWizardController(); 
	Project__c project = controller.getProject();
	system.assertEquals('New', project.Status__c);
    
	controller.step1();
	controller.step2();
	controller.step3();
	
	Test.startTest();
    controller.submit();  
    Test.stopTest();  
	
	/* verify that project is inserted with submitted status */
	List<Project__c>  lstProject = [SELECT Id FROM Project__c];
	System.assertEquals(1, lstProject.size()); 
	System.assertEquals(lstProject[0].Status__c, 'Submitted');       
}

  private static testmethod void testSubmittedProjectException() {
    /*Prepare Test Data */
	Plan__c plan = new Plan__c(/*POPULATE ANY REQUIRED FIELD VALUES HERE*/);
	insert plan;
	// pass plan id for the new project creation
	ApexPages.currentPage().getParameters().put('planid',plan.id);
	Test.setCurrentPage(Page.manage_project);
    projectWizardController controller = new projectWizardController(); 
	Project__c project = controller.getProject();
	project.Status__c = 'New';
    insert project;
	
	Test.startTest();
    controller.submit();  
    Test.stopTest(); 
	
    /* verify that project status is not changed (Because we have already inserted the project above, exception will be thrown) */
    List<Project__c> lstProject = [SELECT Id FROM Project__c];      System.assertEquals(lstProject[0].Status__c, 'Submitted');          
}
 
    
}

 

All Answers

Nayana KNayana K
@isTest
public class projectWizardControllerTest {
  private static testmethod void testSubmittedProjectSuccess() {
	/*Prepare Test Data */
	Plan__c plan = new Plan__c(/*POPULATE ANY REQUIRED FIELD VALUES HERE*/);
	insert plan;
	// pass plan id for the new project creation
	ApexPages.currentPage().getParameters().put('planid',plan.id);
	Test.setCurrentPage(Page.manage_project);
    projectWizardController controller = new projectWizardController(); 
	Project__c project = controller.getProject();
	system.assertEquals('New', project.Status__c);
    
	controller.step1();
	controller.step2();
	controller.step3();
	
	Test.startTest();
    controller.submit();  
    Test.stopTest();  
	
	/* verify that project is inserted with submitted status */
	List<Project__c>  lstProject = [SELECT Id FROM Project__c];
	System.assertEquals(1, lstProject.size()); 
	System.assertEquals(lstProject[0].Status__c, 'Submitted');       
}

  private static testmethod void testSubmittedProjectException() {
    /*Prepare Test Data */
	Plan__c plan = new Plan__c(/*POPULATE ANY REQUIRED FIELD VALUES HERE*/);
	insert plan;
	// pass plan id for the new project creation
	ApexPages.currentPage().getParameters().put('planid',plan.id);
	Test.setCurrentPage(Page.manage_project);
    projectWizardController controller = new projectWizardController(); 
	Project__c project = controller.getProject();
	project.Status__c = 'New';
    insert project;
	
	Test.startTest();
    controller.submit();  
    Test.stopTest(); 
	
    /* verify that project status is not changed (Because we have already inserted the project above, exception will be thrown) */
    List<Project__c> lstProject = [SELECT Id FROM Project__c];      System.assertEquals(lstProject[0].Status__c, 'Submitted');          
}
 
    
}

 
This was selected as the best answer
Bryan TelfordBryan Telford
Hi Nayana,

Amazing! I just made one small tweak to get the test to pass and now have 100% coverage.
[SELECT Id, Status__c FROM Project__c];  

Thank you so much. I learned a lot from your post.

Bryan