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
Sara Burch 20Sara Burch 20 

How to develop a test class

Hello everyone, I successfully create a form of record views of users and now cannot deploy in my production org without a test class. I've searched all day for a sample test that can help explain what I am to do, no luck!

Here is my class code:
 
public class lastViewed{
 
    public Datetime cDT; 
    public String LongDate; 
    public String firstname; 
    public String lastname;    
    public String userid;
 
    private final Chalkboard_Manager__c acct;
 
    public lastViewed(ApexPages.StandardController stdController) {
        this.acct = (Chalkboard_Manager__c)stdController.getRecord();    }
 
                public String getLongDate() {        
                cDT = System.now();        //Format the datetime value to your locale        
                LongDate = cDT.format('dd/MM/yyyy HH:mm');    return LongDate;    
            }
 
        public void updateField() {
        //Get the user info from the current user        
    firstname = System.Userinfo.getFirstName();        
    lastname = System.Userinfo.getLastName();       
    userid = System.Userinfo.getUserId();
 
        //Get the chalkboard record to be updated        
    Chalkboard_Manager__c a = [select Last_Viewed_By__c from Chalkboard_Manager__c where id = :acct.id limit 1];        
 
    //Assign values to Last Viewed By field & update the record        
    a.Last_Viewed_By__c = (firstname + ' ' + lastname + ', ' + getLongDate());        
    update a;    
    }
 
}

 
Best Answer chosen by Sara Burch 20
v varaprasadv varaprasad
Hi Sara,

Please check sample code below : 


@isTest
private class lastViewed_Test{
  @testSetup
  static void setupTestData(){
    test.startTest();
    Chalkboard_Manager__c account_Obj = new Chalkboard_Manager__c(Name = 'Name796', Last_Viewed_By__c = '36');
    Insert account_Obj; 
    test.stopTest();
  }
  static testMethod void test_getLongDate_UseCase1(){
    List<Chalkboard_Manager__c> account_Obj  =  [SELECT Id,Name,Last_Viewed_By__c from Chalkboard_Manager__c];
    System.assertEquals(true,account_Obj.size()>0);
    lastViewed obj01 = new lastViewed(new ApexPages.StandardController(account_Obj[0]));
    obj01.cDT = DateTime.now();
    obj01.LongDate = 'test data';
    obj01.firstname = 'test data';
    obj01.lastname = 'test data';
    obj01.userid = 'test data';
    obj01.getLongDate();
  }
  static testMethod void test_updateField_UseCase1(){
    List<Chalkboard_Manager__c> account_Obj  =  [SELECT Id,Name,Last_Viewed_By__c from Chalkboard_Manager__c];
    System.assertEquals(true,account_Obj.size()>0);
    lastViewed obj01 = new lastViewed(new ApexPages.StandardController(account_Obj[0]));
    obj01.cDT = DateTime.now();
    obj01.LongDate = 'test data';
    obj01.firstname = 'test data';
    obj01.lastname = 'test data';
    obj01.userid = 'test data';
    obj01.updateField();
  }
}



Hope this helps you.
Please let me know in case of any other assistance.


Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

All Answers

Michael BrumleyMichael Brumley
The test class you need to create, when it is run, will call your lastViewed class and should cause at least 75% of its code to excute without causing any errors. This should get you started learning how to create them: 
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
If you're on Trailhead though, you might want to start here:
https://trailhead.salesforce.com/modules/apex_testing

 
v varaprasadv varaprasad
Hi Sara,

Please check sample code below : 


@isTest
private class lastViewed_Test{
  @testSetup
  static void setupTestData(){
    test.startTest();
    Chalkboard_Manager__c account_Obj = new Chalkboard_Manager__c(Name = 'Name796', Last_Viewed_By__c = '36');
    Insert account_Obj; 
    test.stopTest();
  }
  static testMethod void test_getLongDate_UseCase1(){
    List<Chalkboard_Manager__c> account_Obj  =  [SELECT Id,Name,Last_Viewed_By__c from Chalkboard_Manager__c];
    System.assertEquals(true,account_Obj.size()>0);
    lastViewed obj01 = new lastViewed(new ApexPages.StandardController(account_Obj[0]));
    obj01.cDT = DateTime.now();
    obj01.LongDate = 'test data';
    obj01.firstname = 'test data';
    obj01.lastname = 'test data';
    obj01.userid = 'test data';
    obj01.getLongDate();
  }
  static testMethod void test_updateField_UseCase1(){
    List<Chalkboard_Manager__c> account_Obj  =  [SELECT Id,Name,Last_Viewed_By__c from Chalkboard_Manager__c];
    System.assertEquals(true,account_Obj.size()>0);
    lastViewed obj01 = new lastViewed(new ApexPages.StandardController(account_Obj[0]));
    obj01.cDT = DateTime.now();
    obj01.LongDate = 'test data';
    obj01.firstname = 'test data';
    obj01.lastname = 'test data';
    obj01.userid = 'test data';
    obj01.updateField();
  }
}



Hope this helps you.
Please let me know in case of any other assistance.


Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
This was selected as the best answer
Sara Burch 20Sara Burch 20
Thank you very much!