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
Rod TuckerRod Tucker 

System.assert(Methods)

Hey, could anybody differentiate system.assert methods and when to use them. Clear explanation is greatly appreciated
Best Answer chosen by Rod Tucker
NagendraNagendra (Salesforce Developers) 
Hi Rod,

Please find the detailed explanation below.

A simple scenario would be to create a test class +methods in which some work is done (e.g. put something into the DB, update a field or whatever). Once this is done you would use System.Assert functions to check what has been done.
 
For example:-
 
integer i = 0;
integer j = 1;
 
//this would throw an error  and cause your test to fail 
System.assertEquals(i,j);
//this would pass
System.assertNotEquals(1,i);
etc...
 
You may put something into the DB via an insert. You may then expect a trigger to run so you would then reload the item and check that the trigger has fired correctly as certain fields have been updated.
 
Really you could interchange any of the Assert functions dependent on your logic. Up to you. I find it easier to use AssertEquals as I mostly want to check that a proper number of records have been returned or that something I know about has happened and I'm expecting a certain result etc...
 
You are really just trying to create a method to test that some logic (i.e. code - trigger, apex...) is working correctly. Testing is required to cover a certain percentage of a class to enable packaging and a trigger must be tested (greater than 0%) to enable packaging.

Example for system.assert logic :

System.Assert(Today.Year()==2016);
 
Alternatively  System.AssertEquals(2016,Today.Year());
 
Or System.AssertNotEquals(2015,Today.Year());
 
Could call a user function like:-
 
System.Assert(isYear2016());
 
public boolean isYear2016(){
return (Today.Year==2016 ? true : false); 


Please mark my solution as best answer if it helps you.

Best Regards,
Nagendra.P

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Rod,

Please find the detailed explanation below.

A simple scenario would be to create a test class +methods in which some work is done (e.g. put something into the DB, update a field or whatever). Once this is done you would use System.Assert functions to check what has been done.
 
For example:-
 
integer i = 0;
integer j = 1;
 
//this would throw an error  and cause your test to fail 
System.assertEquals(i,j);
//this would pass
System.assertNotEquals(1,i);
etc...
 
You may put something into the DB via an insert. You may then expect a trigger to run so you would then reload the item and check that the trigger has fired correctly as certain fields have been updated.
 
Really you could interchange any of the Assert functions dependent on your logic. Up to you. I find it easier to use AssertEquals as I mostly want to check that a proper number of records have been returned or that something I know about has happened and I'm expecting a certain result etc...
 
You are really just trying to create a method to test that some logic (i.e. code - trigger, apex...) is working correctly. Testing is required to cover a certain percentage of a class to enable packaging and a trigger must be tested (greater than 0%) to enable packaging.

Example for system.assert logic :

System.Assert(Today.Year()==2016);
 
Alternatively  System.AssertEquals(2016,Today.Year());
 
Or System.AssertNotEquals(2015,Today.Year());
 
Could call a user function like:-
 
System.Assert(isYear2016());
 
public boolean isYear2016(){
return (Today.Year==2016 ? true : false); 


Please mark my solution as best answer if it helps you.

Best Regards,
Nagendra.P
This was selected as the best answer
Rod TuckerRod Tucker
Hi Nagendra

That's pretty fair and simple explanation thanks for the help.
Garrett MillerGarrett Miller

Hey @Nagendra  I have a question regarding this if you don't mind. For the user function example, should that function exist right below the System.assert call?