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
Lakshmi SLakshmi S 

How to write test class for getter and setter method ?

Hi Team,

How to write test class for below code.
Controller
-----------------
public with sharing class TPipelineControllerClass {
    
    public static String oppid;
    
    public X360_Pipeline__c pipeline{get;set;}
    private ApexPages.StandardController sc{get;set;}
    public Opportunity opp = new Opportunity();
    public TPipelineControllerClass(ApexPages.StandardController sc){
        this.sc = sc;
        oppid = ApexPages.currentPage().getParameters().get('oppid');
        //system.debug('------'+oppid);
        opp =[select id,name,Account.name from Opportunity where id =:oppid limit 1];
        //system.debug('---'+hr.Name);
        //system.debug('---'+hr.Account.Name);
        pipeline = new X360_Pipeline__c();
        pipeline.Name = opp.Name;
        //system.debug('-------pipeline.Name'+pipeline.Name);
        pipeline.Account_Name__c= opp.AccountId;
        pipeline.Opportunity_Name__c = opp.Id;
    }
    
    public PageReference save(){
        try{
            insert pipeline;
            return new PageReference('/'+pipeline.Id);
        }catch(Exception ex){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, ex.getMessage()));
            return null;
        }
        
        
    }
    
    Public PageReference Cancel(){
        PageReference pr = new PageReference('/'+Schema.SObjectType.Opportunity.getKeyPrefix()+'/o');
        return pr;
        }

}
Test Class :
----------------
@isTest
public class TestTPipelineControllerClass {
    
    @isTest
    private static void test360Pipeline(){
        Test.startTest();
        
        PageReference pr = Page.TPipelineVFPage;
        Test.setCurrentPage(pr);
        
        Opportunity op = new Opportunity();
        try{
            Account acc = new Account(Name='Dummy Account');
            	insert acc;
        	
                op.Name = 'Dummy Oppty';
                op.AccountId = acc.Id;
                op.Lead_Country__c = 'India';
                op.Region__c = 'APAC';
                op.CloseDate = Date.newInstance(2018, 02, 20);
                op.ForecastCategoryName = 'Best Case';
                op.StageName = 'Red Zone / Awarded';
                op.Probability = 90;
                op.Category__c = 'Red Zone / Awarded, Negotiating Contract';
            insert op;
            X360_Pipeline__c pipe = new X360_Pipeline__c();
            pipe.Name = op.Name;
            pipe.Opportunity_Name__c = op.Id;
            pipe.Account_Name__c = op.AccountId;
            insert pipe;  
            
            pr.getParameters().put('id',pipe.Id);
        
            ApexPages.StandardController sc = new ApexPages.StandardController(pipe);
            TPipelineControllerClass pipeline = new TPipelineControllerClass(sc);
            
            pipeline.save();
            pipeline.Cancel();
        
        }
        catch(Exception e){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, e.getMessage()));
        }
        
        System.assert(true);
        System.assertNotEquals(Null, op.id);
        
        Test.stopTest();
    }

}
Can any one please help me how to write test class.

Thanks
Lakshmi

 
Best Answer chosen by Lakshmi S
Sohan Raj GuptaSohan Raj Gupta
Hi Lakshmi,

Try below test class code:
 
@isTest
public class TestTPipelineControllerClass {
    
    @isTest
    private static void test360Pipeline(){
        Test.startTest();
        
        //PageReference pr = Page.TPipelineVFPage;
        //Test.setCurrentPage(pr);
        
        Opportunity op = new Opportunity();
        try{
            Account acc = new Account(Name='Dummy Account');
            	insert acc;
        	
                op.Name = 'Dummy Oppty';
                op.AccountId = acc.Id;
                op.Lead_Country__c = 'India';
                op.Region__c = 'APAC';
                op.CloseDate = Date.newInstance(2018, 02, 20);
                op.ForecastCategoryName = 'Best Case';
                op.StageName = 'Red Zone / Awarded';
                op.Probability = 90;
                op.Category__c = 'Red Zone / Awarded, Negotiating Contract';
            insert op;
            
            X360_Pipeline__c pipe = new X360_Pipeline__c();
            pipe.Name = op.Name;
            pipe.Opportunity_Name__c = op.Id;
            pipe.Account_Name__c = op.AccountId;
            insert pipe;  
            
            //pr.getParameters().put('id',pipe.Id);
        	
            Test.setCurrentPageReference(new PageReference('Page.TPipelineVFPage')); 
			System.currentPageReference().getParameters().put('oppid', op.id);
            
            ApexPages.StandardController sc = new ApexPages.StandardController(pipe);
            TPipelineControllerClass pipelinea = new TPipelineControllerClass(sc);
            
            pipelinea.save();
            pipelinea.Cancel();
        
        }
        catch(Exception e){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, e.getMessage()));
        }
        
        System.assert(true);
        System.assertNotEquals(Null, op.id);
        
        Test.stopTest();
    }

}

Hope this will help you. Let me know if it helped or you need any more assistance. 

Please mark this is as the solution if it solved your purpose.

Thanks,
Sohan Raj Gupta 
 

All Answers

Sohan Raj GuptaSohan Raj Gupta
Hi Lakshmi,

Try below test class code:
 
@isTest
public class TestTPipelineControllerClass {
    
    @isTest
    private static void test360Pipeline(){
        Test.startTest();
        
        //PageReference pr = Page.TPipelineVFPage;
        //Test.setCurrentPage(pr);
        
        Opportunity op = new Opportunity();
        try{
            Account acc = new Account(Name='Dummy Account');
            	insert acc;
        	
                op.Name = 'Dummy Oppty';
                op.AccountId = acc.Id;
                op.Lead_Country__c = 'India';
                op.Region__c = 'APAC';
                op.CloseDate = Date.newInstance(2018, 02, 20);
                op.ForecastCategoryName = 'Best Case';
                op.StageName = 'Red Zone / Awarded';
                op.Probability = 90;
                op.Category__c = 'Red Zone / Awarded, Negotiating Contract';
            insert op;
            
            X360_Pipeline__c pipe = new X360_Pipeline__c();
            pipe.Name = op.Name;
            pipe.Opportunity_Name__c = op.Id;
            pipe.Account_Name__c = op.AccountId;
            insert pipe;  
            
            //pr.getParameters().put('id',pipe.Id);
        	
            Test.setCurrentPageReference(new PageReference('Page.TPipelineVFPage')); 
			System.currentPageReference().getParameters().put('oppid', op.id);
            
            ApexPages.StandardController sc = new ApexPages.StandardController(pipe);
            TPipelineControllerClass pipelinea = new TPipelineControllerClass(sc);
            
            pipelinea.save();
            pipelinea.Cancel();
        
        }
        catch(Exception e){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, e.getMessage()));
        }
        
        System.assert(true);
        System.assertNotEquals(Null, op.id);
        
        Test.stopTest();
    }

}

Hope this will help you. Let me know if it helped or you need any more assistance. 

Please mark this is as the solution if it solved your purpose.

Thanks,
Sohan Raj Gupta 
 
This was selected as the best answer
Lakshmi SLakshmi S
Hi Sohan Raj Gupta ,

It's working fine.
Thank you very much for your quick response.

Regards
Lakshmi.