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
Ramin MohammadiRamin Mohammadi 

Need help with test code for VF controller extension

public with sharing class whitespace{
public String AccountID{get;set;}
public String PAccountID{get;set;}
public List<Account> IPlist{get;set;}


public whitespace(ApexPages.StandardController controller){
    AccountID = ApexPages.currentPage().getParameters().get('id');
           
    PAccountID = [select ParentId from Account where ID = :AccountID].ParentId;
    if (PAccountID == null){
        IPList = [select Name,(select Product_Family__c from Product_Releases_del__r) from Account a where ParentID =: AccountID];
    }
    else {
        IPList = [select Name,(select Product_Family__c from Product_Releases_del__r) from Account a where ParentID =: PAccountID];   
    }
    
}
    
    
        
        
}

This works, I just have no idea what writing test code entails
Best Answer chosen by Ramin Mohammadi
Himanshu ParasharHimanshu Parashar
Hi Ramin,

Following code will work.
 
@isTest
private class whitespace_Test {

static testMethod void whitespacemethod(){

Account acc = new Account(Name='Test');
insert acc;

Account childaccount = new Account(Name='Child',parentid=acc.id);
insert childaccount;

ApexPages.StandardController stc = new ApexPages.StandardController controller(acc);
ApexPages.currentPage().getParameters().put('id',acc.id);
whitespace objW = new whitespace(stc);


ApexPages.StandardController stc = new ApexPages.StandardController controller(acc); ApexPages.currentPage().getParameters().put('id',childaccount.id); 
whitespace objW = new whitespace(stc);
}

}



 

All Answers

Himanshu ParasharHimanshu Parashar
Hi Ramin,

Following code will work.
 
@isTest
private class whitespace_Test {

static testMethod void whitespacemethod(){

Account acc = new Account(Name='Test');
insert acc;

Account childaccount = new Account(Name='Child',parentid=acc.id);
insert childaccount;

ApexPages.StandardController stc = new ApexPages.StandardController controller(acc);
ApexPages.currentPage().getParameters().put('id',acc.id);
whitespace objW = new whitespace(stc);


ApexPages.StandardController stc = new ApexPages.StandardController controller(acc); ApexPages.currentPage().getParameters().put('id',childaccount.id); 
whitespace objW = new whitespace(stc);
}

}



 
This was selected as the best answer
Ramin MohammadiRamin Mohammadi
Im getting an unexpected token at line 12 'controller'
Ramin MohammadiRamin Mohammadi
got it. you're a bro Himanshu
RAJNagRAJNag
Hi  Ramin Mohammadi

Can u share the Page code for the reference.

Thanks
Raj.