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
VSK98VSK98 

Test code coverage for VF Page

Hi All,

I have written test class for one of my VF Page & covered upto 73% but i failed to cover morethan 75%. Here is my VF Page & Apex Classes
 
<apex:page controller="DataTableEditRemoveController">
<apex:form id="form" >
<apex:pageBlock title="Accounts">
<apex:pageMessages ></apex:pageMessages>
<apex:pageBlockTable value="{!accs}" var="acc">
<apex:column >
<apex:outputLink title="" value="/{!acc.id}/e?retURL=/apex/{!$CurrentPage.Name}" style="font-weight:bold">Edit</apex:outputLink>&nbsp;|&nbsp;
<apex:commandLink action="{!DeleteAccount}" onclick="return confirm('Are you sure?')" value="Del">
    <apex:param value="{!acc.Id}" name="accountid" assignTo="{!SelectedAccountId}"/>
</apex:commandLink>
</apex:column>
<apex:column value="{!acc.Name}"/>
<apex:column value="{!acc.BillingStreet}"/>
<apex:column value="{!acc.BillingCity}"/>
<apex:column value="{!acc.BillingPostalCode}"/>
<apex:column value="{!acc.BillingCountry}"/>    
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>      
</apex:page>

Here is Controller
 
public class DataTableEditRemoveController {

    public String getRow() {
        return null;
    }

public List<Account> accs { get; set; }

//used to get a hold of the account record selected for deletion
public string SelectedAccountId { get; set; }

public DataTableEditRemoveController() {
//load account data into our DataTable
LoadData();
}

@Testvisible
private void LoadData() {
system.debug('Enter In the Constructor*****');
accs = [Select id, name, BillingStreet, BillingCity, BillingPostalCode, BillingCountry from Account limit 20];
}

public PageReference DeleteAccount()
{
System.debug('SelectedAccountId: '+SelectedAccountId);
accs = [select id,name, BillingStreet, BillingCity, BillingPostalCode, BillingCountry from Account where id=:SelectedAccountId];
System.debug('SIZEOFACC: '+accs.size());
if(accs.size()>0 && accs[0]!= null){
delete accs;
System.debug('DELSIZEOFACC: '+accs.size());
SelectedAccountId = null;

}

//refresh the data
LoadData();
    return null;
}


}

Here is 29 th line is not covered ...............


Here is My Test Class
 
@istest
public class DataTableEditRemoveController_Test{
static testmethod void Method_Test(){

    Account acc = new Account();
    acc.name = 'Test';
    acc.Fax = '555555';
    acc.BillingStreet = 'Korlagunta';
    acc.BillingCity = 'Tirupati';
    acc.BillingPostalCode = 'Tiruapti';
    acc.BillingCountry = 'INDIA';
    insert acc;
    
   // Delete acc;
  system.debug('ACCID******'+acc.id);  
    Test.starttest();
    pagereference pageref = page.Edit_Del_Hyperlink_Fun;
    Test.SetCurrentPageReference(pageref);
    pageref.getParameters().put('SelectedAccountId', String.valueOf(acc.Id));
  string STRID = apexpages.currentpage().getparameters().put('SelectedAccountId',acc.id);
   System.assertEquals(STRID, acc.Id, 'ID\'s should match');
   system.debug('PARAMID******'+apexpages.currentpage().getparameters().put('SelectedAccountId',acc.id)); 
    DataTableEditRemoveController contrll = new DataTableEditRemoveController ();
    contrll.LoadData();
    contrll.DeleteAccount();
    list<Account> a =[select id,name, BillingStreet, BillingCity, BillingPostalCode, BillingCountry from Account where id=:STRID];
    system.debug('LSTACCID******'+a.size());
    Delete a;
    Test.stoptest();

I have checked the debugs, SelectedAccountId its coming in test class but not in main controller..........I am struck here .......Please give me your valuable suggestions.......

Adv Thnx
VSK98
Best Answer chosen by VSK98
swati_sehrawatswati_sehrawat
Can you try:

DataTableEditRemoveController contrll = new DataTableEditRemoveController ();
contrll.SelectedAccountId = acc.id
contrll.LoadData();
contrll.DeleteAccount();
 

All Answers

swati_sehrawatswati_sehrawat
Can you try:

DataTableEditRemoveController contrll = new DataTableEditRemoveController ();
contrll.SelectedAccountId = acc.id
contrll.LoadData();
contrll.DeleteAccount();
 
This was selected as the best answer
hitesh90hitesh90
Try to use following code.

Test Class:
@istest
public class DataTableEditRemoveController_Test{
    static testmethod void Method_Test(){    
        Account acc = new Account();
        acc.name = 'Test';
        acc.Fax = '555555';
        acc.BillingStreet = 'Korlagunta';
        acc.BillingCity = 'Tirupati';
        acc.BillingPostalCode = 'Tiruapti';
        acc.BillingCountry = 'INDIA';
        insert acc;
        
        // Delete acc;
        system.debug('ACCID******'+acc.id);  
        Test.starttest();
        pagereference pageref = page.Edit_Del_Hyperlink_Fun;
        Test.SetCurrentPageReference(pageref);
        pageref.getParameters().put('SelectedAccountId', String.valueOf(acc.Id));
        string STRID = apexpages.currentpage().getparameters().put('SelectedAccountId',acc.id);
        System.assertEquals(STRID, acc.Id, 'ID\'s should match');
        system.debug('PARAMID******'+apexpages.currentpage().getparameters().put('SelectedAccountId',acc.id)); 
        DataTableEditRemoveController contrll = new DataTableEditRemoveController ();
        contrll.LoadData();
        contrll.SelectedAccountId = acc.id;
        contrll.getRow();
        contrll.DeleteAccount();
        list<Account> a =[select id,name, BillingStreet, BillingCity, BillingPostalCode, BillingCountry from Account where id=:STRID];
        system.debug('LSTACCID******'+a.size());
        Delete a;
        Test.stoptest();    
    }
}

 
VSK98VSK98
Hi Swathi,

It's perfectly working now..........But i want to why 
pageref.getParameters().put('SelectedAccountId', String.valueOf(acc.Id));

the above line not useful here.............Can you explain ?????????

Thnx
VSK 98
swati_sehrawatswati_sehrawat
getParameters() and putParameters() work when you are passing values from URL, here SelectedAccountId was getting assigned from page and than passed to controller
VSK98VSK98
Wht i understood means.when you are passing id's from param we should follow Controller.Selectedid............when you are passing ID's from URL should use PutParameters()........ Is it right?
Ankur Saini 9Ankur Saini 9
Hi VSK98
try this:-
 
@istest
public class DataTableEditRemoveController_Test{
static testmethod void Method_Test(){

    Account acc = new Account();
    acc.name = 'Test';
    acc.Fax = '555555';
    acc.BillingStreet = 'Korlagunta';
    acc.BillingCity = 'Tirupati';
    acc.BillingPostalCode = 'Tiruapti';
    acc.BillingCountry = 'INDIA';
    insert acc;
    
   // Delete acc;
  system.debug('ACCID******'+acc.id);  
    Test.starttest();
    pagereference pageref = page.DATA;
    Test.SetCurrentPageReference(pageref);
    pageref.getParameters().put('SelectedAccountId', String.valueOf(acc.Id));
    string STRID = apexpages.currentpage().getparameters().put('SelectedAccountId',acc.id);
    System.assertEquals(STRID, acc.Id, 'ID\'s should match');
    system.debug('PARAMID******'+apexpages.currentpage().getparameters().put('SelectedAccountId',acc.id)); 
    DataTableEditRemoveController contrll = new DataTableEditRemoveController ();
    contrll.LoadData();
    contrll.SelectedAccountId=acc.Id;
    contrll.DeleteAccount();
    contrll.getRow();
    list<Account> a =[select id,name, BillingStreet, BillingCity, BillingPostalCode, BillingCountry from Account where id=:STRID];
    system.debug('LSTACCID******'+a.size());
    Delete a;
    Test.stoptest();
}
}

Thanks 
Ankur Saini
http://mirketa.com