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
suneel raosuneel 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;
  }
  
}

}
Shantanu SrivastavaShantanu Srivastava
Hi Suneel,

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
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for test classes
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

 
@isTest 
public class UgcontrollerTest 
{
	static testMethod void testMethod1() 
	{
		Account testAccount = new Account();
		testAccount.Name='Test Account' ;
		insert testAccount;

		Test.StartTest(); 

			ApexPages.currentPage().getParameters().put('id',testAccount.id);
			
			Ugcontroller obj = new Ugcontroller();
			String str = obj.getBirthdateDOW();
			obj.FindBirthdateDOW();
			obj.Ugpage4();
			obj.ugpage3();
			obj.ugpage2();
			obj.ugpage1();

			obj.selectedyear ='2016';
			obj.dat = system.today();
			
			
			Ugcontroller.Sampledate sampleObj = new Ugcontroller.Sampledate();
			List<SelectOption> lst = sampleObj.getyearOptions();
			sampleObj.cancel();
			sampleObj.save();
			sampleObj.getpdfpage();
			
			
			Ugcontroller.ExamplePageController exp = new Ugcontroller.ExamplePageController();
			exp.FindBirthdateDOW();
			
		Test.StopTest();
	}
}
Let us know if this will help you
 
suneel raosuneel rao
Thanks for the replay Shantanu & Amit.
Thanks for the Links and the class it's really helped me how to write test class.
Thank you.