You need to sign in to do that
Don't have an account?
Test Class for trigger
Hi,
I am new to salesforce and have writte the following trigger but not able to write test class for it.
Can anyone help me with the code for its test class??
Trigger
trigger increaseCount on Employee__c (after insert)
{
List<Employee__c> EmployeeToUpdate = new List<Employee__c>{};
for(Employee__c emp : Trigger.new)
{
Company__c company =[ select Number_of_Employees__c from Company__c where Id= :emp.Company__c ];
if (company.Number_of_Employees__c==Null)
{
company.Number_of_Employees__c=1;
}
else
{company.Number_of_Employees__c =company.Number_of_Employees__c+1;}
update company;
}
}
Hi,
First things first: dont use SOQL query, DML statements inside for loop. Refer these posts for more info.
Sample Test class:
thanks Abhi,
I have written this test class on my own, but code coverage is coming out to be 0.
I am not able to confgure the part i am missing.
Test Class
@isTest
private class IncreaseCountTest
{
static testMethod void myUnitTest()
{
//insert a company record
Company__c cmp = new Company__c();
cmp.Name='abc';
insert cmp;
Employee__c emp= new Employee__c();
//use the company record id to populate the Employee's company__c field on employee
emp.Company__c=cmp.id;
cmp = [SELECT Number_of_Employees__c FROM Company__c WHERE Id = :emp.Company__c];
//insert an employee record
emp.Name='sam';
emp.DOB__c=System.today()-1500;
emp.DOJ__c=System.today();
emp.Company__c='cmp.id';
emp.Department__c='Finance';
insert emp;
update cmp;
integer test=1;
System.assertEquals(test, cmp.Number_of_Employees__c );
}
}
try this
Here is your test method: