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
DoubleADoubleA 

System.assert methods (for test methods) - need explanation

Hello,

 

I know it seems like a dumb question but I'm trying to familiarize myself with testing apex code with apex language.  

 

I am unsure when to use system.assertequals(x,y), system.assert(condition) and system.assertnotequals(x,y) and I have noticed it being used quite a bit.  I've looked at the user guide and and this link http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods, but still require more of a plain english explanation on when and why these methods are used.

 

Can anyone help me on this.

 

Much thanks!

Best Answer chosen by Admin (Salesforce Developers) 
Richie DRichie D

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.

 

Good luck!

 

 

 

 

All Answers

Richie DRichie D

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.

 

Good luck!

 

 

 

 

This was selected as the best answer
DoubleADoubleA
Thanks richie, that's plain and clear to me.  Do you have any examples of when you used System.assert(logic), and how it helped in your testings?
Richie DRichie D

Glad to help.

 

Just use System.Assert({a statement}) ;

 

e.g.

 

System.Assert(Today.Year()==2009);

 

Alternatively  System.AssertEquals(2009,Today.Year());

 

Or System.AssertNotEquals(2008,Today.Year());

 

Could call a user function like:-

 

System.Assert(isYear2009());

 

public boolean isYear2009(){

return (Today.Year==2009 ? true : false); 

DoubleADoubleA
Awesome, that is fairly simple.  So I could use this just to check on whether the code is doing what it is supposed to do or the opposite!
ShawnFShawnF

I'm building a vf page that will show details of a parent object, and then iterate over a list of related child records that have a certain status (where status__c != 'Leased').

 

I'm looking for an explanation on how to use System.Assert in regards to the related list of child objects.

 

Meaning, I have a query method that should result in a list of related child records.

 

So...I write a testmethod that calls my query method.

 

When I call my query method and it returns a list how, then, do I use System.Assert to assert that my query method has returned the list I expect?

 

All the examples I have seen of System.Assert seem to check one value against another. In my case, I'm not checking one value against another, I just want to verify that the query method has returned a list of related child objects where status__c != 'Leased'.

 

If there are no related child records where status__c != 'Leased', that is fine. I just won't display anything out on the vf page - that section will just be blank. Likewise, I don't really care how many related child records are returned in the list either. Meaning, the query method could return 1 record in the list or it could return 100 records in the list. Doesn't matter.

 

Furthermore, I'm using the vf page as part of a Force.com Site. Since it is public, I'm not allowing any saving, updating or editing of the either the parent record being viewed or the related child objects being viewed. The vf page is only intended to query the db for related child records (where status__c != 'Leased') and iterate over them on the vf page in read-only mode. No data manipulation happening.

Avinash YAvinash Y
Hi Shawn

Did you find how to do it? If so can u post it. 
DebjaniDebjani
Hi All,

I have a question regarding this syntax,
In this system.assertEquals(3,item.size()) what is the '3' for? Please explain with a short example.

Also, I though in the below script String[1] means it should allow only one element. But I was able to save this code in a Test class.
String[] words= new String[1];
words[1] = 'one';
words[2] = 'two';
etc...
Bridget Hudacs 6Bridget Hudacs 6
Hi Lilly,

In your system.assertEquals statement, "3" is your expected outcome and you're testing it against the return of your function "item.size". Whatever item.size is used to calcuate, if the outcome is 3, then your statement is true. If it's not, then you'll get an error.

Re your String: I'm a bit rusty on this, but I believe your test method (words[1], words[2]) is introducing different variables to test. Each test should bring back only 1 value using your insert value ('one', 'two').