You need to sign in to do that
Don't have an account?
Help in writting a test class
Hi Guys
Please help me out in writing a test class for the following contoller. I am calling this controller in a VF page. Here is my apex class. I am just attaching some part of my class so if i get the test class for the below code i can use this as a reference and i can write for the remeining part of the class. Appreciate your help
public class SSOWcontroller {
private final SSOW__c ssow;
private final HVoIP__c hvoip;
private final VoIP__c voip;
public SSOWcontroller(ApexPages.StandardController stdController) {
ssow = [ Select Id, IsDeleted, Name, CreatedDate, CreatedById, LastModifiedDate, LastModifiedById,
SystemModstamp, Site_City__c, Site_State__c, Site_Street__c, Site_Thoroughfair__c, Site_Zip_Code__c,
NPA_NXX__c, Division__c, Primary_Contact__c FROM SSOW__c WHERE ID = :ApexPages.currentPage().getParameters().get('id')];
hvoip = [ Select Id, OwnerId, IsDeleted, Name, CreatedDate, CreatedById, LastModifiedDate,
LastModifiedById, SystemModstamp, Qualifying_Product__c, Did_you_ensure_that_hosted__c,
Did_you_indicate_the_quantity__c, Did_you_ensure_that_the_bandwidth__c FROM HVoIP__c WHERE SSOW__c = :ssow.id];
voip = [ Select Id, OwnerId, IsDeleted, Name, CreatedDate, CreatedById, LastModifiedDate, LastModifiedById,
SystemModstamp, Qualifying_Product__c, Did_you_ensure_that_Voip_and_Data__c, Own_public_IP_Space__c,
Routing_protocol_other_than_Static__c FROM VoIP__c WHERE SSOW__c = :ssow.id];
}
public SSOW__c getssow() {
return ssow;
}
public HVoIP__c gethvoip() {
return hvoip;
}
public VoIP__c getvoip() {
return voip;
}
private void saveallobjects() {
update ssow;
update hvoip;
update voip;
}
public PageReference saveAll() {
saveallobjects();
PageReference pr4 = new PageReference('/apex/SmartSOW?Id=' + ssow.Id);
pr4.setRedirect(true);
return pr4;
}
}
I;ll take a stab at it...
Hi, Thanks for your help. I wrote the code as you said and this is the error it is throwing. Please let me know where did i make wrong. I highlighed in Red where the error is throwing. Here is the error
@isTest
Private class ssowControllerTest{
static SSOW__c tsow;
static HVoIP__c tHvoip;
Private static testMethod void doTest(){
//Get a SOW record - You can also create one if it does not exist which is best practice
tSOW = new SSOW__c();
tSOW.name = 'test ssow';
tSOW.Site_City__c = 'Littlerock';
tSOW.Site_State__c = 'AR';
tSOW.NPA_NXX__c = '123456';
tSOW.Primary_Contact_Phone__c = '1234567890';
tSOW.Primary_Contact_Email__c = 'test@proposal.com.full';
tSOW.IT_Contact_Phone__c = '234567891';
insert tSOW;
thvoip = new HVoIP__c();
thvoip.Alarm_or_elevator_lines__c = 'yes';
thvoip.Are_all_features_and_LD_quoted_properly__c = 'yes';
thvoip.CAF_Completed__c = 'Yes';
thvoip.Site_Address__c = 'test';
thvoip.Site_City__c = 'Fremont';
thvoip.Site_State__c = 'CA';
thvoip.SSOW__c = tsow.id;
thvoip.NPA_NXX__c = '123456';
insert thvoip;
//Test Variables
SSOW__c testSOW;
HVoIP__c testHVoIP;
VoIP__c testVoIP;
PageReference pr = new PageReference('Page.SmartSOW');
Test.setCurrentPage(pr);
system.currentPageReference().getParameters().put('id',tSOW.id);
SSOWController aTest = new SSOWController();
testSOW = aTest.getssow();
system.assertNotEquals(0,testSOW.size());
testHVoIP = aTest.gethvoip();
system.assertNotEquals(0,testHVoIP.size());
testVoIP = aTest.getvoip();
system.assertNotEquals(0,testVoIP.size());
aTest.saveallobjects();
aTest.saveAll();
}
}
Is the controller that you mentioned in your very first post still in existance and with the same name?
yes, its the same name. The name of the controller is SSOWcontroller
Ahhh,
This line in your class constructor
public SSOWcontroller(ApexPages.StandardController stdController) {
}
requires you to pass a controller.
I am not the greatest with controllers so someone may have to help here.
In my classes that use this I have an additional constructor without requirements i.e:
public SSOWcontroller() {
init();
}
public void init(){
//do stuff here
}
In each of the constructors I call an init() method and do all my set up there and not in the consturctor.
I am interested to learn what to pass here......
Try adding
ApexPages.StandardController sc = new ApexPages.standardController(tSOW);
and then: SSOWController aTest = new SSOWController(sc);