+ Start a Discussion
neeedhelpneeedhelp 

How to write a test class

hello Everyone,

   I'm writing  a test class for a simple controller and it gave me 85% of code covered........ I want 100% of it how can I get it

 

@istest
private class sharewidgettest{
public static testmethod void testsharewidget(){

ApexPages.StandardController Controller;
sharewidget wid = new sharewidget(controller);
wid.save();

}
}

 

and this is my class

 

public class ShareWidget {

public string widgetid{get;set;}

public Share_Widget__c element{get;set;}
public ShareWidget(ApexPages.StandardController controller)
{

widgetid = apexpages.currentpage().getparameters().get('id');
system.debug('iiiiid'+widgetid);

if(widgetid == null)
{
element = new Share_Widget__c();
}
else
{
element = [select Name,Widget_Code__c from Share_Widget__c where id = :widgetid];
}
}

public pagereference Save() {

if(widgetid == null)
{

insert element;
}
else {

update element;

}
system.debug('ele'+element);
pagereference widpage = new apexpages.standardcontroller(element).view();
widpage.setredirect(true);
return widpage;
}

}

  

Best Answer chosen by Admin (Salesforce Developers) 
SamuelDeRyckeSamuelDeRycke

ApexPages.currentPage().getParameters().put('id',share.id);

 

^ That only has an impact if you re-intialize your contstructor, Try this

 

@istest
private class sharewidgettest {
    public static testmethod void testsharewidget() {
        ApexPages.StandardController Controller;
        sharewidget wid = new sharewidget(controller);
        Share_Widget__c share = new Share_Widget__c(Name = 'Sri', Widget_Code__c = 'ffhfgfg');
        insert share;
        wid.save();
//set up data ApexPages.currentPage().getParameters().put('id', share.id); sharewidget wid = new sharewidget(controller); //restart wid.save(); } }

 

 

 

All Answers

SamuelDeRyckeSamuelDeRycke

You have conditional logic, so your test method should have scenarios for all conditional branches of your logic.

 

In addition to your current test, you could insert a sharewidget object, and put its ID as pageparameter before you re-initialize your constructor and do save() again.

 

apexpages.currentpage().getparameters().put('id',NewInsertedObject.ID); 

 Give that a try, its a simple controller and you will learn most of you write the code yourself.

 

 

 

neeedhelpneeedhelp

 

 even tried that way but unable to cover the update part

 

@istest
private class sharewidgettest{
public static testmethod void testsharewidget(){
ApexPages.StandardController Controller;
sharewidget wid = new sharewidget(controller);
Share_Widget__c share = new Share_Widget__c(Name='Sri',Widget_Code__c='ffhfgfg');
insert share;
ApexPages.currentPage().getParameters().put('id',share.id);
Share_Widget__c share1 = new Share_Widget__c(Name='Arun',Widget_Code__c = 'dfdfdf');
insert share1;
wid.save();
}
}

 

 giving the same 85% code

SamuelDeRyckeSamuelDeRycke

ApexPages.currentPage().getParameters().put('id',share.id);

 

^ That only has an impact if you re-intialize your contstructor, Try this

 

@istest
private class sharewidgettest {
    public static testmethod void testsharewidget() {
        ApexPages.StandardController Controller;
        sharewidget wid = new sharewidget(controller);
        Share_Widget__c share = new Share_Widget__c(Name = 'Sri', Widget_Code__c = 'ffhfgfg');
        insert share;
        wid.save();
//set up data ApexPages.currentPage().getParameters().put('id', share.id); sharewidget wid = new sharewidget(controller); //restart wid.save(); } }

 

 

 

This was selected as the best answer
neeedhelpneeedhelp

Can u explain what ecatly this does in updating the values

 

ApexPages.currentPage().getParameters().put('id', share.id);
sharewidget wid1 = new sharewidget(controller); //restart
wid1.save();

 

Thanks for the solution

SamuelDeRyckeSamuelDeRycke

Well, its pretty basic, if we look at your constructor:

 

Public ShareWidget(ApexPages.StandardController controller) {
    widgetid = apexpages.currentpage().getparameters().get('id');
if (widgetid == null) {
element = new Share_Widget__c();
} else {
element = [select Name, Widget_Code__c from Share_Widget__c where id = : widgetid];
}
}

 So this is exectud only when your object is initialized by doing

 

new sharewidget(controller);

As you have conditional code around that wedgetId, you need to have your test code run through both the if block, and the else block. You can only do that by once running your constructor with no Id being set in the page parameters, and once where you do set the Id.  Whieras you usually would get the page parameters, you can also set them which is particualry useful in test code (I'm actually not sure if you can set them outside test context).

 

ApexPages.currentPage().getParameters().put('id', share.id);

 So only after doing this, the wedgetId in your page will not be null, for a Shareobject initialised AFTER doing this. We have to insert a valid ID, so that your SOQL statement would have a result object.

 

Do you understand ? (if not, say so)

 

 

 

neeedhelpneeedhelp

Thanks a ton for the explanation........:)