+ Start a Discussion
Saad Ahmad 27Saad Ahmad 27 

How to write test class for "Page reference" method? Looking for advise.

My Code is: 
public class AssociateSOExtController {

    public Quote quote { get; set; }
    
    public AssociateSOExtController() {
        
        String quoteId = ApexPages.currentPage().getParameters().get('id');
        quote = [SELECT Id, Name, Service_Repair__c FROM quote WHERE Id =: quoteId];
        
    }
    
    public PageReference associate () {
        
        Repair_History__c SORepair = new Repair_History__c ( Id = quote.Service_Repair__c, Quote__c = quote.Id);
        
        try {
        	Database.update(SORepair);
        } catch (Exception error) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Error while associating.' + error.getMessage()));
        }
        
        PageReference page = new PageReference('/' + quote.Id);
        
        return page.setRedirect(true);
    }
    
    public PageReference cancel () {
        PageReference page = new PageReference('/' + quote.Id);
        return page.setRedirect(true);        
    }
    
}

Thank you all for your help!
Best Answer chosen by Saad Ahmad 27
Ashif KhanAshif Khan
Hi Saad,
 
@isTest
public class AssociateSOExtControllerTest {
    public static testMethod void tst() {
        Opportunity op = new Opportunity();
        op.Name='aa';
        op.StageName='Qualification';
        op.CloseDate=system.today();
        insert op;
        
        Repair_History__c ss=new Repair_History__c();
        ss.name='ss';
        insert ss;
        
        Quote qq= new Quote();
        qq.Service_Repair__c=ss.id;
        qq.name='qq';
        qq.OpportunityId=op.Id;
        insert qq;
        ApexPages.currentPage().getParameters().put('id',qq.id);
        
        Test.StartTest();
        AssociateSOExtController sh1 = new AssociateSOExtController();
        
        sh1.associate();
        sh1.cancel();
        Test.stopTest(); 
        
        
    }
}

 

All Answers

Nayana KNayana K
@isTest
public class thecontrollerTests
{

	@isTest
    public static void testSuccess() 
	{
        PageReference pageRef = Page.YOUR_CURRENT_PAGE_NAME;
        Test.setCurrentPage(pageRef);
      
        AssociateSOExtController controller = new AssociateSOExtController();
        
		/*Insert test sample quote : Let's say objQuote is the inserted instance */
		/*Insert test sample Repair History for the above quote: Let's say objService is the inserted instance */
		
        ApexPages.currentPage().getParameters().put('Id', objQuoteId);
      
        // Instantiate a new controller with all parameters in the page
        controller.associate();

        // Verify that the success 
        system.assertEquals(true, [SELECT COUNT() FROM Repair_History__c WHERE Id =: objService.Id AND Quote__c =: objQuote.Id]>0);
		
    }
	
	@isTest
    public static void testException() 
	{
        PageReference pageRef = Page.YOUR_CURRENT_PAGE_NAME;
        Test.setCurrentPage(pageRef);
      
        AssociateSOExtController controller = new AssociateSOExtController();
        
		/*Insert test sample quote : Let's say objQuote is the inserted instance */
		/*DONT INSERT REPAIR HISTORY */
		
		ApexPages.currentPage().getParameters().put('Id', objQuoteId);
      
        // Instantiate a new controller with all parameters in the page
        controller.associate();

        // Verify that the failure 
        system.assertEquals(false, [SELECT COUNT() FROM Repair_History__c WHERE Id =: objService.Id AND Quote__c =: objQuote.Id]>0);
		
    }
}

 
Ashif KhanAshif Khan
Hi Saad,
 
@isTest
public class AssociateSOExtControllerTest {
    public static testMethod void tst() {
        Opportunity op = new Opportunity();
        op.Name='aa';
        op.StageName='Qualification';
        op.CloseDate=system.today();
        insert op;
        
        Repair_History__c ss=new Repair_History__c();
        ss.name='ss';
        insert ss;
        
        Quote qq= new Quote();
        qq.Service_Repair__c=ss.id;
        qq.name='qq';
        qq.OpportunityId=op.Id;
        insert qq;
        ApexPages.currentPage().getParameters().put('id',qq.id);
        
        Test.StartTest();
        AssociateSOExtController sh1 = new AssociateSOExtController();
        
        sh1.associate();
        sh1.cancel();
        Test.stopTest(); 
        
        
    }
}

 
This was selected as the best answer
Nayana KNayana K
Oops, ignore my code (It is wrong. My bad). Use above one.
Saad Ahmad 27Saad Ahmad 27
Thanks Nayana and Ashif! Really appreciate your help.