An effort to bring a revolution in Salesforce.

Tuesday, 26 July 2016

Salesforce System.assert(Methods)

System.assert()

System.assert() 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);
}
, , , ,

No comments:

Post a Comment

Get Gmail, Docs, Drive, and Calendar for business