You need to sign in to do that
Don't have an account?
suneel rao
How to write the test class
Hi all,
Can anyone help me in writting test class for this.
public class Ugcontroller {
public String getBirthdateDOW() {
return null;
}
public PageReference FindBirthdateDOW() {
return null;
}
public String selectedyear{get;set;}
public Date dat{get;set;}
public ID ugId{get;set;}
public UnderGradute__c ugform{
get {
if (ugform== null)
ugform = new UnderGradute__c();
return ugform;
}
set;
}
public PageReference Ugpage4() {
return page.UG_page4;
}
public PageReference ugpage3() {
return page.UG_page3;
}
public PageReference ugpage2() {
return page.UG_page2;
}
public PageReference ugpage1() {
return page.UG_Page1;
}
public Ugcontroller () {
}
public class Sampledate {
public Sampledate() {
}
}
public List<SelectOption> getyearOptions() {
List<SelectOption> countryOptions = new List<SelectOption>();
countryOptions.add(new SelectOption('','-None-'));
for(integer i=2016;i<=2022;i++)
{
string s = string.valueof(i);
countryOptions.add(new SelectOption(s,s));
}
return countryOptions;
}
public PageReference cancel() {
return null;
}
public PageReference save() {
system.debug(this.ugform);
try {
ugform.Year_Enrollment__c = String.valueOf(selectedyear);
ugform.Date_of_Birth_ug__c = Date.valueof(dat);
upsert ugform;
} catch (DMLException e) {
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new Undergraduate.'));
return null;
}
Pagereference ref = new Pagereference('/apex/UG_congrats?id=' +ugform.id);
ref.setRedirect(true);
// if successfully inserted new lform, then displays the thank you page.
return ref;
}
public PageReference getpdfpage() {
this.ugId = ApexPages.currentPage().getParameters().get('id');
Pagereference ref = new Pagereference('/apex/UG_PDF_Page?id=' +this.ugId);
ref.setRedirect(true);
// if successfully inserted new lform, then displays the thank you page.
return ref;
}
public with sharing class ExamplePageController {
public ExamplePageController(Ugcontroller controller) {
}
public UnderGradute__c ugform{get; set;}
public String StringDate {get; set;}
public String StringDateDOW {get; set;}
public String BirthdateDOW {get; set;}
// create a temporary contact
public ExamplePageController() {
ugform = new UnderGradute__c();
}
// find the day of the week for Birthdate. Date.valueOf can be used with Date fields
public PageReference FindBirthdateDOW() {
try {
BirthdateDOW = DateTime.newInstance( Date.valueOf(ugform.Date_of_Birth_ug__c), Time.newInstance(0, 0, 0, 0) ).format('EEEE');
} catch (exception e) {
BirthdateDOW = '';
}
return null;
}
}
}
Can anyone help me in writting test class for this.
public class Ugcontroller {
public String getBirthdateDOW() {
return null;
}
public PageReference FindBirthdateDOW() {
return null;
}
public String selectedyear{get;set;}
public Date dat{get;set;}
public ID ugId{get;set;}
public UnderGradute__c ugform{
get {
if (ugform== null)
ugform = new UnderGradute__c();
return ugform;
}
set;
}
public PageReference Ugpage4() {
return page.UG_page4;
}
public PageReference ugpage3() {
return page.UG_page3;
}
public PageReference ugpage2() {
return page.UG_page2;
}
public PageReference ugpage1() {
return page.UG_Page1;
}
public Ugcontroller () {
}
public class Sampledate {
public Sampledate() {
}
}
public List<SelectOption> getyearOptions() {
List<SelectOption> countryOptions = new List<SelectOption>();
countryOptions.add(new SelectOption('','-None-'));
for(integer i=2016;i<=2022;i++)
{
string s = string.valueof(i);
countryOptions.add(new SelectOption(s,s));
}
return countryOptions;
}
public PageReference cancel() {
return null;
}
public PageReference save() {
system.debug(this.ugform);
try {
ugform.Year_Enrollment__c = String.valueOf(selectedyear);
ugform.Date_of_Birth_ug__c = Date.valueof(dat);
upsert ugform;
} catch (DMLException e) {
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new Undergraduate.'));
return null;
}
Pagereference ref = new Pagereference('/apex/UG_congrats?id=' +ugform.id);
ref.setRedirect(true);
// if successfully inserted new lform, then displays the thank you page.
return ref;
}
public PageReference getpdfpage() {
this.ugId = ApexPages.currentPage().getParameters().get('id');
Pagereference ref = new Pagereference('/apex/UG_PDF_Page?id=' +this.ugId);
ref.setRedirect(true);
// if successfully inserted new lform, then displays the thank you page.
return ref;
}
public with sharing class ExamplePageController {
public ExamplePageController(Ugcontroller controller) {
}
public UnderGradute__c ugform{get; set;}
public String StringDate {get; set;}
public String StringDateDOW {get; set;}
public String BirthdateDOW {get; set;}
// create a temporary contact
public ExamplePageController() {
ugform = new UnderGradute__c();
}
// find the day of the week for Birthdate. Date.valueOf can be used with Date fields
public PageReference FindBirthdateDOW() {
try {
BirthdateDOW = DateTime.newInstance( Date.valueOf(ugform.Date_of_Birth_ug__c), Time.newInstance(0, 0, 0, 0) ).format('EEEE');
} catch (exception e) {
BirthdateDOW = '';
}
return null;
}
}
}
You can refer the Salesforce documentation to write your own test classes. Please refer to https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm and https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
Keep in mind to cover all Positive, Negative scenarios. Try following all Salesforce best practices like creating your test data in the test class itself instead of querying for it.
Please post here if you face problems with code coverage or any other issue.
Thanks,
Shantanu
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
3) http://amitsalesforce.blogspot.com/search/label/Test%20Class
4) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html
Let us know if this will help you
Thanks for the Links and the class it's really helped me how to write test class.
Thank you.