0

below is my code

Public Class Testmanage{
    public Testmanage(ApexPages.StandardController controller) {
    }
    public pageReference load()
    {
        if(ApexPages.currentPage().getParameters().get('id')!= '' || ApexPages.currentPage().getParameters().get('id') != null)
        {
             Id id = ApexPages.currentPage().getParameters().get('id');
             List<Test__c>lstTest= [Select Phone__c from Test__c where id=:id LIMIT 1] ;
             if(!lstTest.isEmpty())
             {
                 PageReference pageRef = new PageReference('/apex/OppoAgr?id='+lstTest[0].Phone__c);
                 pageRef.setRedirect(true);
                 return pageRef;
             }
             return null;          
        }    
        return null;
    }
}

Below is my test class

@istest
public class Test_Data1 {static testmethod void Testmeth1(){ 
    Test__c tst=new Test__c(name='test',phone__C='3223233');
    insert tst;

    ApexPages.StandardController sc = new ApexPages.standardController(tst);
    Testmanage cs=new Testmanage(sc);
    pagereference pr = page.Demopage;
    test.setCurrentPage(pr);
    ApexPages.currentPage().getParameters().put('id',tst.id);
    cs.loadAction();
}
static testmethod void Testmeth1(){
    Test__c tst=new Test__c(name='test',phone__C='3223233');
   // insert tst;
            ApexPages.StandardController sc = new ApexPages.standardController(tst);
    Testmanage cs=new Testmanage(sc);
    pagereference pr = page.Demopage;
    test.setCurrentPage(pr);
    ApexPages.currentPage().getParameters().put('id',null);
    cs.load();

}

I am not able to cover last return null statement

3
  • Why do you think that is? Any thoughts on how you could prevent that, or if it even matters? Commented Nov 18, 2016 at 10:59
  • Do you have a compelling reason to cover that last line for 100%? I'd say your first/redundant return null is the culprit though Commented Nov 18, 2016 at 11:45
  • As i am new to coding ,i dont understand how to cover this.Please suggest
    – dem
    Commented Nov 18, 2016 at 11:53

2 Answers 2

1

If the aim is to test (and also cover) the body of the method, in your test you will need to insert a Test__c object and add its Id value to the page parameters so the body of the method is executed:

Test__c t = new Test(Phone__c = '123 456 789');
insert t;
ApexPages.currentPage().getParameters().put('id', t.Id);

Testmanage c = ...;
PageReference pr = c.load();

In the method itself, take care to escape the phone string value as it may contain spaces. A simple way of doing that is to not build the URL yourself but leverage the PageReference class that has correct escaping built in:

PageReference pageRef = new PageReference('/apex/OppoAgr');
pageRef. getParameters().put('id', lstTest[0].Phone__c);

PS

With more code posted in the question, it looks like this is a controller extension for Test__c and in that case it is better to get the Id value from the standard controller:

public Class Testmanag {

    private ApexPages.StandardController sc;

    public Testmanage(ApexPages.StandardController sc) {
        this.sc = sc;
    }

    public PageReference load() {
        if (s.getId() != null) {
            PageReference pr = Page.OppoAgr;
            Test__c t = [select Phone__c from Test__c where Id = :sc.getId()];
            pr.getParameters().put('id', t.Phone__c != null ? t.Phone__c : '');
            return pr;      
        }    
        return null;
    }
}

Unfortunately to get tests to pass it is still necessary to run a query rather than use the standard controller reference.

0

You can directly call the load() method without setting the ApexPages.currentPage parameter so that it will directly return the null value and in your normal testmethod cover the code with setting the parameter.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .