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
GangaGanga 

I want To write a Test Class For an Employee class .... urgent

Hi Friends
I am new to salesforce so i am struggling abit ...with the coding so please kindly help ....... 
i want to write a test for an employee object and the class name is employee this class written by me to use in  Vf pages 
 i have created a employee object using this class in vf pages..... so now i want to test the class so i am getting error ...plase help class i wrote is 

public with sharing Class Employee
{

public String Name {get; set;}
public String Qualification {get; set;}
public Integer Age {get; set;}
public Integer Salary{get; set;}
Public String Desig{get; set;}

public list<Employee__c> getEmployeeList{get;set;}

public String getName() {
        return 'Employee';
    }
public pageReference save() {    
  Employee__c E = new Employee__c (
                           Name__c = Name,
                          
                           Desig__c = Desig,
                           Age__c = Age ,
                           Salary__c = Salary,
                           Qualification__c = Qualification   );
                    insert E;
                   
                    
                    PageReference nextPage = new PageReference('/' + E.Id);
                   
        return nextPage;
                    }
                    public PageReference saveAndNew() { 
          Employee__c Emp = new Employee__c (
                           Name__c = Name,
                          
                           Desig__c = Desig,
                           Age__c = Age ,
                           Salary__c = Salary,
                           Qualification__c = Qualification   );
         insert Emp;
      return (new ApexPages.StandardController(new Employee__c())).edit();     
     
    
    } 

public pageReference Clear()
{
pageReference page = new pageReference('https://ap1.salesforce.com/a03/o');
page.setRedirect(true);
return page;
}

public List<Employee__c> EmployeeList()
  {

     getEmployeeList = [select Name__c, Salary__c from Employee__c];
     return getEmployeeList;

}

public PageReference GetList()
{
     EmployeeList();
     return null;
}
}


and the Test class  I  wrote is
 
ERROR :System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name__c]: [Name__c]



And i have only one required field in employee object 
Name__c  and data type is    Text(20)
rest of the fields are 
Desig__c 
  Age__c 
Salary__c 
Qualification__c


@istest
public class Testclass
{

public static testmethod void Employee()
{

Employee e = new Employee();

Employee__c empc = new Employee__c();

empc.Name__c = 'Test Name';
empc.Desig__c = 'Testy Designation';
empc.Age__c = 18;
empc.Salary__c = 100;
empc.Qualification__c = 'BE';

insert empc;

e.save();
e.saveAndNew();
e.Clear();
e.GetList();

}
}



Thanks in Advance 
Vinita_SFDCVinita_SFDC
Hello,

The field Name__c will accept a string, so include the value in quotes. Like:

Name__c = 'Name'

Similarly check data types of other fields and assign values accordingly.
GangaGanga
I checked the data types and i am already included in quotes ...... still its getting Error
Norm Sherman SFNorm Sherman SF
Between Employee e = new Employee(); and e.save();, where are you setting the values for your Employee class?