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
sfdc-Lsfdc-L 

Pls give me the small example for test classess using system assert and asserequals.

Hi,

 

i just want to understand writing the test classes using assert equals and assert.so kindly give me the small example in vf.

 

 

Thanks

bob_buzzardbob_buzzard

Put in the context of a simple class

 

public class MyClass
{
   public name;

   public MyClass(String inName)
   {
      name=inName;
   }
   
}

 

Then you'd use an assert to confirm the behaviour of the constructor like so:

 

public static testMethod void testClass()
{
   MyClass tstClass=new MyClass('test');
   System.assertEquals(tstClass.Name, 'test');
}

 

 

sfdc-Lsfdc-L

Thanks Bob,

i would like to know that, for every time creating test classes using assertequals,we need to define constructor?

and why we are using system.assertequals and assert for test classess? so pls let me know so that i can understand easily.

 

 

  

bob_buzzardbob_buzzard

You don't need to define constructors etc.  Asserts are a way of confirming that the code is behaving as expected.  For example, you may invoke a method that writes information to the database.  In your test class you would execute the method, retrieve the record from the database and use asserts to confirm that the record field values are as expected.

 

There are two aspects to unit testing on Force.com

 

- coverage, ensuring that your tests execute at least 75% of the code you wish to deploy.  However, all this test is that the code runs without failing.  

- confirmation - this is where asserts come in.  Your tests should confirm that the code behaves as expected, effectively adhering to contracted behaviour.  The benefits of confirmation are (a) ensuring the code works as you think it should (b) catching any changes that would stop it behaving as expected (and thus breaking downstream code)