You need to sign in to do that
Don't have an account?
![andrew1.3889672359072202E12 andrew1.3889672359072202E12](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
Test method on a visualforce extension
I have the following simple visualforce extension, but I'm having a hard time getting the test method to asert properly. Here's the class.
public class extUserProduct{
private final User_Product__c UserProduct;
List<User_Product__c> lstUserProduct;
Contact UserProductNew;
public extUserProduct(ApexPages.StandardSetController controller) {
lstUserProduct = (List<User_Product__c>)[Select Name, Id, Custom_User_Fee__c, Custom_Currency_Code__c,
Contact_Full_Name__c, Billing_Start_Date__c, Active_Start_Date__c, Contact__c,Product__c, Product_Provisioning_Date__c
From User_Product__c
where Product_Setup_Complete__c=FALSE AND Contact__c=:ApexPages.currentPage().getParameters().get('id')];
UserProductNew = [Select Id, Name From Contact Where Id =: ApexPages.currentPage().getParameters().get('id')];
}
public List<User_Product__c> getlstUserProduct(){
return lstUserProduct;
}
public Contact getUserProductNew(){
return UserProductNew;
}
public PageReference save() {
try{
update lstUserProduct;
}
catch(DmlException ex){
ApexPages.addMessages(ex);
return Null;
}
return (new ApexPages.StandardController(UserProductNew)).view();
}
}
I've started my test, but can't figure out how to assert the class to get any test coverage. Here's the test so far.
@isTest private class TestUserProd {
static testMethod void TestUserProd() {
City__c ct = new City__c(name='Toronto');
insert ct;
Account a = new Account(name='Test', Type='Customer');
insert a;
Contact c = new Contact(AccountID=a.id, FirstName='Test', LastName='Test', phone='555-555-5555');
insert c;
User_Product__c up = new User_Product__c(Account__c=a.id, Contact__c=c.id, Product__c='Product');
insert up;
Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
User u = new User(Alias = 'standt', Email='standarduser@test.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@test.com');
insert u;
System.runAs(u) {
PageReference pageRef = Page.ProductUpdate;
Test.setCurrentPageReference(pageRef);
ApexPages.currentPage().getParameters().put('contactId', c.Id);
}
}
}
Thoughts?
public class extUserProduct{
private final User_Product__c UserProduct;
List<User_Product__c> lstUserProduct;
Contact UserProductNew;
public extUserProduct(ApexPages.StandardSetController controller) {
lstUserProduct = (List<User_Product__c>)[Select Name, Id, Custom_User_Fee__c, Custom_Currency_Code__c,
Contact_Full_Name__c, Billing_Start_Date__c, Active_Start_Date__c, Contact__c,Product__c, Product_Provisioning_Date__c
From User_Product__c
where Product_Setup_Complete__c=FALSE AND Contact__c=:ApexPages.currentPage().getParameters().get('id')];
UserProductNew = [Select Id, Name From Contact Where Id =: ApexPages.currentPage().getParameters().get('id')];
}
public List<User_Product__c> getlstUserProduct(){
return lstUserProduct;
}
public Contact getUserProductNew(){
return UserProductNew;
}
public PageReference save() {
try{
update lstUserProduct;
}
catch(DmlException ex){
ApexPages.addMessages(ex);
return Null;
}
return (new ApexPages.StandardController(UserProductNew)).view();
}
}
I've started my test, but can't figure out how to assert the class to get any test coverage. Here's the test so far.
@isTest private class TestUserProd {
static testMethod void TestUserProd() {
City__c ct = new City__c(name='Toronto');
insert ct;
Account a = new Account(name='Test', Type='Customer');
insert a;
Contact c = new Contact(AccountID=a.id, FirstName='Test', LastName='Test', phone='555-555-5555');
insert c;
User_Product__c up = new User_Product__c(Account__c=a.id, Contact__c=c.id, Product__c='Product');
insert up;
Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
User u = new User(Alias = 'standt', Email='standarduser@test.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@test.com');
insert u;
System.runAs(u) {
PageReference pageRef = Page.ProductUpdate;
Test.setCurrentPageReference(pageRef);
ApexPages.currentPage().getParameters().put('contactId', c.Id);
}
}
}
Thoughts?
ExtUserProduct ctrl=new ExtUserProduct(new ApexPages.StandardController(c));
You can then execute methods and assert the responses are as expected - for example:
List<User_Product__c> ups=ctrl.getlstUserProduct();
System.assertNotEquals(ups, null);
Constructor not defined: [extUserProduct].<Constructor>(ApexPages.StandardController)
Any suggestions? Thanks!