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
MattwestMattwest 

Need help on a test class

Hello all,
I'm having a hard time grasping this concept. I've struggled through a few test classes based on creating new records. I'm trying to learn by example, and I just need help. Can someone show me what a test class for this trigger might look like. And if you have time, tell me why it works. Thanks in advance for your help!
trigger ReassignCall on Service_call__c (before insert, before update) {
    for (service_call__c sc: Trigger.new) {
        if (sc.assigned_to__c != null) {
            sc.OwnerId = sc.assigned_to__c;
            sc.assigned_to__c = null;
        }
    }
}

 
Amit Chaudhary 8Amit Chaudhary 8
Please try below code
@isTest 
private class ReassignCallTest
{
	 static testMethod void ReassignCallTest() 
	 {
		User usr = [select firstname from user where id=:userinfo.getuserid()];
		
		Test.StartTest();
			service_call__c serviceCall = new service_call__c();
			//serviceCall.FieldName ='value';
			// Add all required fields
			serviceCall.assigned_to__c = usr.id;
			insert serviceCall;
		Test.StopTest();
	 }
}
Please follow below blog for test class best pratice
http://amitsalesforce.blogspot.in/2015/06/salesforce-testing-best-practice.html

Some points for test class below :

1.Test class must start with @isTest annotation if class class version is more than 25 
2.Test environment support @testVisible,@testSetUp as well 
3.Unit test is to test particular piece of code working properly or not .
4.Unit test method takes no argument ,commit no data to database ,send no email ,flagged with testMethod keyword .
5.To deploy to production atleat 75% code coverage is requird and al test case should pass .
6.System.debug statemet ar enot counted as a part of apex code coverage .
7.Test method and test classes are not counted as a part of code coverage .
9.We should not focus on the  percentage of code coverage ,we should make sure that every use case should coverd including positive, negetive,bulk and single record .
10.Single Action -To verify that the the single record producess the correct an dexpected result .
11.Bulk action -Any apex record triger ,class or extention must be invoked for 1-200 records .
12.Positive behaviour : Test every expected behaviour occurs through every expected permutation , i,e user filled out every correctly data and not go past the limit .
13.Negetive Testcase :-Not to add future date ,Not to stecify negetive amount.
14.Restricted User :-Test whether a user with restricted access usded in yur code .
15.Conditional and ternary operator are not considered executed unless both positive and negetive branches are executed .
16.Unit test are class methods that verify whether a particular piece of code is error free or not .
17.Test class should be annoted with isTest .
18.isTest annotation with test method  is equivalant to  testMethod keyword .
19.Test method should static and no void return type .
20.Test class and method default access is private ,no matter to add access specifier .
21.classes with isTest annotation can't be a interface or enum .
22.Test method code can't be invoked by non test request .
23.Stating with salesforce API 28.0 test method can not reside inside non test classes .
24.@Testvisible annotaion to make visible private methods inside test classes.
25.Test method can not be used to test webservice call out .Instaed use call out mock .
26.You cann't  send email fron test method.
27.User,profile,organisation,AsyncApexjob,Corntrigger,RecordType,ApexClass,ApexComponent,ApexPage we can access without (seeAllData=true) .
28.SeeAllData=true will not work for API 23 version eailer .
29.Acessing static resource test records in test class e,g List<Account> accList=Test.loadData(Account,SobjectType,'ResourceName').
30.Create TestFactory class with isTest annotation to exclude from organisation code size limit .
31.@testSetup to create test records once in a method  and use in every test method in the test class .
32.We can run unit test for a specic class,set of classes and all classes .
33.We can run unit test by using Salesforce Standrad UI,Force.com IDE ,Console ,API.
34.Maximum number of test classes run per 24 hour of period is  not grater of 500 or 10 multiplication of test classes of your organisation.
35.As apex runs in system mode so the permission and record sharing are not taken into account . So we need to use system.runAs to enforce record sharing .
36.System.runAs will not enforce user permission or field level permission .
37.Every test to runAs count against the total number of DML issued in the process .

Please mark this as solution if this will help you
MattwestMattwest
Thank you, that's a lot of great info!
Amit Chaudhary 8Amit Chaudhary 8
Hi Mattwest,

Please follow below two post for more information on Test classes :-
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html
http://amitsalesforce.blogspot.in/2015/06/salesforce-testing-best-practice.html

Please try below code for your test class
@isTest 
private class ReassignCallTest
{
	 static testMethod void ReassignCallTest() 
	 {
		User usr = [select firstname from user where id=:userinfo.getuserid()];
		
		Test.StartTest();
			service_call__c serviceCall = new service_call__c();
			//serviceCall.FieldName ='value';
			// Add all required fields
			serviceCall.assigned_to__c = usr.id;
			insert serviceCall;
		Test.StopTest();
	 }
}

Please mark this as solution if this will help you

Thanks
Amit Chaudhary
amit.salesforce21@gmail.com