You need to sign in to do that
Don't have an account?

Testin Custom Controller...!!!
Hi
I am facing difficulty in writing a test class for a custom controller, Any help of any kind is greatly appreciated...
please find below the code....
public class UploadAppController {
public Contact ct {get; set;}
public string ctId;
public Enrollment_Opportunity__c application {get; set;}
public string applicationId;
public Enrollment_Opportunity__c apps {get; set;}
// public Blob document {get; set;}
// public String contentType {get; set;}
// public String fileName {get; set;}
public UploadAppController() {
Id id = ApexPages.currentPage().getParameters().get('id');
apps = (id == null) ? new Enrollment_Opportunity__c() :
[Select id,Name,Application_Status__c,Age2__c,RecordTypeId,Applicant__c,Documents_Created__c,
Primary_Phone_Number__c,Gender1__c,Mailing_Address__c,City6__c,State2__c
from Enrollment_Opportunity__c where id =: id ];
application = apps;
this.applicationId = apps.id;
Contact co = [Select Id,firstName,lastName from Contact where Id =:apps.Applicant__c];
ct=co;
this.ctId=co.id;
}
public string getctId()
{
return ctId;
}
public string getapplicationId()
{
return applicationId;
}
public PageReference admissiondoc()
{
update(apps);
// if(application.Documents_Created__c==True){
Id id = ApexPages.currentPage().getParameters().get('id');
PageReference P = new PageReference('/apex/documentinfo?Id='+id);
p.setRedirect(true);
return p;
// }
// else{
// update(apps);
// PageReference P = new PageReference('/apex/submission');
// p.setRedirect(true);
// return p;
// }
}
public PageReference SaveExit()
{
update(apps);
PageReference P = new PageReference('/apex/personalinfo');
return p;
}
public PageReference GoNext()
{
update(apps);
Id id = ApexPages.currentPage().getParameters().get('id');
PageReference P = new PageReference('/apex/PartB?Id='+id);
return p;
}
public PageReference save() {
try {upsert(apps);
} catch(System.DMLException e) {ApexPages.addMessages(e);
return null;
}
// After Save, navigate to the default view page:
return (new ApexPages.StandardController(apps)).view();
}
public List<Enrollment_Opportunity__c> getApplication1()
{
Id id = ApexPages.currentPage().getParameters().get('id');
List<Enrollment_Opportunity__c> Applications = [Select id,Name,Age2__c,RecordTypeId,
I_have_scheduled_a_visit_on__c, I_will_submit_a_recorded_audition__c, Living_Status_Requested__c
from Enrollment_Opportunity__c where Applicant__c =: id and RecordTypeId='012T00000004cfd'];
return Applications ;
}
public List<Enrollment_Opportunity__c> getApplication2()
{
Id id = ApexPages.currentPage().getParameters().get('id');
List<Enrollment_Opportunity__c> Applications = [Select id,Name,Age2__c,RecordTypeId,Application_Status__c,Applicant__c,Documents_Created__c,Primary_Phone_Number__c,Gender1__c,Mailing_Address__c,City6__c,State2__c, Email_Address__c,Zip_postal_code__c,Country5__c,Home_Telephone1__c,Cellular_Telephone__c
from Enrollment_Opportunity__c where Applicant__c =: id and RecordTypeId='012T00000004cfY'];
return Applications ;
}
}
HI Try This,
i'm sure.
public static testMethod void test_ex1(){
contact c= new contact(lastname='radhika');(give here values which are required fields)
insert c;
Enrollment_Opportunity__c en=new Enrollment_Opportunity__c(name='fgf',recordtypeid='012T00000004cfd',Applicant__c=c.id);(give here values which are required fields)
insert en;
Enrollment_Opportunity__c en1=new Enrollment_Opportunity__c(name='fgf1',recordtypeid='012T00000004cfY',Applicant__c=c.id);(give here values which are required fields)
insert en1;
ApexPages.currentPage().getParameters().put('id',en.Id);
UploadAppController u=new UploadAppController();
u.getctId();
u.getapplicationId();
ApexPages.currentPage().getParameters().put('id',en.Id);
u.admissiondoc();
u.SaveExit();
u.GoNext();
u.save();
ApexPages.currentPage().getParameters().put('id',en.Id);
u.getApplication1();
ApexPages.currentPage().getParameters().put('id',en1.Id);
u.getApplication2();
}
Thanks
Radhika
Radhika_y@dskvap.com
All Answers
HI Try This,
i'm sure.
public static testMethod void test_ex1(){
contact c= new contact(lastname='radhika');(give here values which are required fields)
insert c;
Enrollment_Opportunity__c en=new Enrollment_Opportunity__c(name='fgf',recordtypeid='012T00000004cfd',Applicant__c=c.id);(give here values which are required fields)
insert en;
Enrollment_Opportunity__c en1=new Enrollment_Opportunity__c(name='fgf1',recordtypeid='012T00000004cfY',Applicant__c=c.id);(give here values which are required fields)
insert en1;
ApexPages.currentPage().getParameters().put('id',en.Id);
UploadAppController u=new UploadAppController();
u.getctId();
u.getapplicationId();
ApexPages.currentPage().getParameters().put('id',en.Id);
u.admissiondoc();
u.SaveExit();
u.GoNext();
u.save();
ApexPages.currentPage().getParameters().put('id',en.Id);
u.getApplication1();
ApexPages.currentPage().getParameters().put('id',en1.Id);
u.getApplication2();
}
Thanks
Radhika
Radhika_y@dskvap.com
Thank you ,so much....It worked perfectly fine for me....
Hi , I am writing a standard controller for the contact record and below is code and I have written a test method too...but it is not working....I am not able to cover the part from red color....
Can some one guide ...?
public class ContactController {
public Contact contact {get; set;}
public string applicationId;
public Student_Information__c studentinfo{get;set;}
public ContactController(ApexPages.StandardController controller)
{
contact = new contact();
contact.LastName = userInfo.getLastName();
contact.FirstName = userInfo.getFirstName();
// String ContactId = userInfo.getContactId();
// How do I test this part from here....
User userInfo1 = [Select ContactId, Id from User where Id =: userInfo.getUserId()];
contact = [Select Id,firstName,lastName,Anticipated_Term_Start_Date__c,Email,Program_of_Interest__c,HomePhone,MobilePhone,Birthdate,
Contact_Status__c,Gender__c,OtherPhone,LeadSource,MailingStreet,MailingCity,MailingCountry,MailingPostalCode,MailingState,
OtherStreet,OtherCity,OtherCountry, OtherPostalCode,OtherState,What_are_you_applying_to__c from Contact where Id =: userInfo1.ContactId];
Enrollment_Opportunity__c App = [Select id from Enrollment_Opportunity__c where Applicant__c =: Contact.Id limit 1];
this.applicationId = App.id;
}
/* public ContactController()
{
Id id = ApexPages.currentPage().getParameters().get('id');
contact = (id == null) ? new Contact() :
[SELECT id,name,phone FROM Contact WHERE id = :id];
}*/
public string getapplicationId ()
{
return this.applicationId;
}
public PageReference Back()
{
upsert(contact);
PageReference P = new PageReference('/apex/personalinfo?Id='+Contact.Id);
p.setRedirect(true);
return p;
}
public PageReference SaveNext()
{
//try
// {
upsert(contact);
// contact.LastName = userInfo.getLastName();
// contact.FirstName = userInfo.getFirstName();
// User userInfo1 = [Select ContactId, Id from User where Id =: userInfo.getUserId()];
// contact = [Select Id,firstName,lastName,Anticipated_Term_Start_Date__c,Email,Program_of_Interest__c,HomePhone,MobilePhone,Birthdate,
// Contact_Status__c,Gender__c,OtherPhone,LeadSource,MailingStreet,MailingCity,MailingCountry,MailingPostalCode,MailingState,
// OtherStreet,OtherCity,OtherCountry, OtherPostalCode,OtherState,What_are_you_applying_to__c from Contact where Id =: userInfo1.ContactId];
PageReference P = new PageReference('/apex/campacademy?Id='+Contact.Id);
p.setRedirect(true);
// }
// catch(System.DMLException e)
// {
// ApexPages.addMessages(e);
//
//}
return p;
}
public PageReference Next()
{
//try
// {
update(contact);
// User userInfo1 = [Select ContactId, Id from User where Id =: userInfo.getUserId()];
// contact = [Select Id,firstName,lastName,Anticipated_Term_Start_Date__c,Email,Program_of_Interest__c,HomePhone,MobilePhone,Birthdate,
// Contact_Status__c,Gender__c,OtherPhone,LeadSource,MailingStreet,MailingCity,MailingCountry,MailingPostalCode,MailingState,
// OtherStreet,OtherCity,OtherCountry, OtherPostalCode,OtherState,What_are_you_applying_to__c from Contact where Id =: userInfo1.ContactId];
if(contact.What_are_you_applying_to__c=='Interlochen Summer Arts Camp'){
PageReference p = new PageReference('/apex/NewCampApp?Id='+contact.id);
p.setRedirect(true);
return p;
}
else{
PageReference p = new PageReference('/apex/NewApplication?Id='+contact.id);
p.setRedirect(true);
return p;
}
}
/*
public PageReference Exit()
{
//try
// {
upsert(contact);
PageReference P = new PageReference('/apex/NewCampApp?Id='+Contact.Id);
p.setRedirect(true);
// }
// catch(System.DMLException e)
// {
// ApexPages.addMessages(e);
//
//}
return p;
}
*/
public PageReference SaveExit()
{
//try
// {
upsert(contact);
PageReference P = new PageReference('/apex/customerportal');
// }
// catch(System.DMLException e)
// {
// ApexPages.addMessages(e);
//
//}
return p;
}
public PageReference submit()
{
//try
// {
update(contact);
studentinfo=[Select Id from Student_Information__c where Contact__c =: Contact.Id];
PageReference r = new PageReference('https://c.cs0.visual.force.com/apex/personalinfo1?Id='+Studentinfo.Id);
// }
// catch(System.DMLException e)
// {
// ApexPages.addMessages(e);
//
//}
return r;
}
public PageReference Camp()
{
update(contact);
PageReference r = new PageReference('https://c.cs0.visual.force.com/apex/NewCampApp?Id='+contact.id);
r.setRedirect(true);
return r;
}
public PageReference Academy()
{
update(contact);
PageReference r = new PageReference('https://c.cs0.visual.force.com/apex/NewApplication?Id='+contact.Id);
r.setRedirect(true);
return r;
}
public static testMethod void test_ex1(){
contact c= new contact(lastname='radhika'); //give required fields
insert c;
Enrollment_Opportunity__c en=new Enrollment_Opportunity__c(name='fgf',Applicant__c=c.id); //give required fields
insert en;
Student_Information__c s=new Student_Information__c(contact__c=c.id);
insert s;
ApexPages.StandardController sc=new ApexPages.StandardController(c);
ContactController u=new ContactController(sc);
u.getapplicationId();
u.Academy();
u.Camp();
u.submit();
u.SaveExit();
u.Next();
u.SaveNext();
u.Back();
}
Yeah ....but How can I pass the userinfo.getUserId()...in the test method....because the class is returning me an current user i.e admin and giving me an error as list has no errors when fetching for the ContactID from the User record...