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
ezdhanhussainezdhanhussain 

Test class 75% covered help needed in covering remaining percent

Hi i am trying to learn unit testing in salesforce. Below is the code of my controller class. The lines marked as red are not getting covered, can any one suggest me how to do it ? Thanks in advance.

public class RadioButton {
public List<contact> selectcon=new List<contact>();
contact con;
public string selectconid;
Public List<contact> getAllContacts()
{
List<contact> allcons = [Select Id,FirstName,LastName,Email,Phone from Contact LIMIT 10];
return allcons;
}
Public void selectcon()
{
string selectconid = System.currentPagereference().getParameters().get('conid');

con = [Select Id,FirstName,LastName,Email,Phone from Contact where Id=:selectconid limit 1];

selectcon.add(con);

}
Public List<contact> getselectedContact()
{
return selectcon;
}

}

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

Hi Hussain,

 

Below is the sample code for your test class.

 

Test Class:

@istest
public class TestRadioButton{
    Private Static testmethod void TestRadioButton(){
        Account a=new account();
        a.Name = 'Test Name';
        insert a;
        
        Contact c =new Contact();
        c.lastname='test';
        c.accountid=a.id;
        insert c;
        
        System.currentPagereference().getParameters().put('conid',c.id);
        RadioButton objRadioButton = new RadioButton();
        objRadioButton.getAllContacts();
        objRadioButton.selectcon();
        objRadioButton.getselectedContact();
    }
}

 

 

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

All Answers

NTPNTP

Hussain,

 

Are you setting a contact id to the current page parameter 'conid'? If not you will have to set it in your test class.

It is trying to get a contact record where it matches with the page parameter Id, which I guess is returning null.

 

hitesh90hitesh90

Hi Hussain,

 

Below is the sample code for your test class.

 

Test Class:

@istest
public class TestRadioButton{
    Private Static testmethod void TestRadioButton(){
        Account a=new account();
        a.Name = 'Test Name';
        insert a;
        
        Contact c =new Contact();
        c.lastname='test';
        c.accountid=a.id;
        insert c;
        
        System.currentPagereference().getParameters().put('conid',c.id);
        RadioButton objRadioButton = new RadioButton();
        objRadioButton.getAllContacts();
        objRadioButton.selectcon();
        objRadioButton.getselectedContact();
    }
}

 

 

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

This was selected as the best answer
ezdhanhussainezdhanhussain

Yes i am getting system.query exception:List has no rows for assignment to sobject error.