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
R R MR R M 

Test Class need, Please do help

HI Folks, 

Need test class for below apex class.
 
public with sharing class ApexStartController {
    public ApexStartController() {
        params = System.currentPageReference().getParameters();
        // System.debug(params);
        execScript = false;
    }
    /** Parameter */
    public Map<String,String> params { get; set; }
    /** Question */
    public String q {
        get {
            if (q == null) {
                q = params.get('q');
                if (q != null)
                    q = EncodingUtil.urlDecode(q, 'UTF-8');
            }
            return q;
        }
        set; 
    }
    /** Apex Class */
    public String c {
        get { return params.get('a'); }
        set; 
    }
    /** Apex Method */
    public String m {
        get { return params.get('m'); }
        set; 
    }
    /** Parameter Name */
    public String n {
        get { return params.get('n'); }
        set; 
    }
    /** Parameter Value */
    public String v {
        get { return params.get('v'); }
        set; 
    }

    public Boolean execScript { get; set; }
    
    public PageReference doStart() {
        execScript = true;
        return null;
    }

    public PageReference doCancel() {
    PageReference pg;
        String r = params.get('r');
        if (r != null && r.length() > 0)

            pg=new PageReference(r);
            pg.setRedirect(true);
        return pg;
    }
}

I tried but getting error Attempt to de-reference a null object, and with 44% code coverage, please see below code and do need full help. 
 
@isTest 
public class ApexStartController_Test {
static testMethod void Apexstart() { 


Test.StartTest(); 
PageReference pageRef = Page.GenerateDocslink; // Add your VF page Name here
Test.setCurrentPage(pageRef);
ApexStartController Ln1 = new ApexStartController();
Ln1.doStart();
Ln1.doCancel();
Test.StopTest();
}
}

Thanks InAdvance. 
Best Answer chosen by R R M
sfdcMonkey.comsfdcMonkey.com
First update your doCancel method, always use starting and closing bracket for If condition :
 
public PageReference doCancel() {
    PageReference pg;
        String r = params.get('r');
        if (r != null && r.length() > 0){
            pg=new PageReference(r);
            pg.setRedirect(true);
          }  
        return pg;
    }

and use below test class:
@isTest 
public class ApexStartController_Test {
    static testMethod void Apexstart() { 
        
        
        Test.StartTest(); 
        PageReference pageRef = Page.GenerateDocslink; // Add your VF page Name here
        ApexPages.currentPage().getParameters().put('r','test');
        
        Test.setCurrentPage(pageRef);
        ApexStartController Ln1 = new ApexStartController();
        string testQ = Ln1.q;
        string testC = Ln1.c;
        string testM = Ln1.m;
        string testN = Ln1.n;
        string testV = Ln1.v;
        Ln1.doStart();
        Ln1.doCancel();
        Test.StopTest();
    }
}

Thanks 
let us know if it helps you 

All Answers

sfdcMonkey.comsfdcMonkey.com
First update your doCancel method, always use starting and closing bracket for If condition :
 
public PageReference doCancel() {
    PageReference pg;
        String r = params.get('r');
        if (r != null && r.length() > 0){
            pg=new PageReference(r);
            pg.setRedirect(true);
          }  
        return pg;
    }

and use below test class:
@isTest 
public class ApexStartController_Test {
    static testMethod void Apexstart() { 
        
        
        Test.StartTest(); 
        PageReference pageRef = Page.GenerateDocslink; // Add your VF page Name here
        ApexPages.currentPage().getParameters().put('r','test');
        
        Test.setCurrentPage(pageRef);
        ApexStartController Ln1 = new ApexStartController();
        string testQ = Ln1.q;
        string testC = Ln1.c;
        string testM = Ln1.m;
        string testN = Ln1.n;
        string testV = Ln1.v;
        Ln1.doStart();
        Ln1.doCancel();
        Test.StopTest();
    }
}

Thanks 
let us know if it helps you 
This was selected as the best answer
R R MR R M
Thanks Piyush, its worked.