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
bonny mankotiabonny mankotia 

How To Check Different Assert Methods in this code?

public with sharing class AssertEx 
{
    
    public integer sum()
    {
         integer a =10;
            integer b =20;
         integer c;
         c=a+b;
         system.debug('======'+c);
         //system.assert();
         //system.assertequals();
         //System.assertNotEquals();
         return c;
    }
}
Best Answer chosen by bonny mankotia
suresh sanneboina 4suresh sanneboina 4
Hi,

integer a =10;
integer b =20;
integer c;
c=a+b;
system.debug('======'+c);
system.assert(c!=(a+b),'Its an Error');
system.assertequals(c,(a-b),'Sum Not Equals');
System.assertNotEquals(c,(a+b),'Sum Equals');

System.assert(condition, msg)
Asserts that the specified condition is true. If it is not, a fatal error is returned that causes code execution to halt.
eg:system.assert(c!=(a+b),'Its an Error');

System.assertEquals(expected, actual, msg)
Asserts that the first two arguments are the same. If they are not, a fatal error is returned that causes code execution to halt.
eg:system.assertequals(c,(a-b),'Sum Not Equals');

System.assertNotEquals(expected, actual, msg)
Asserts that the first two arguments are different. If they are the same, a fatal error is returned that causes code execution to halt.
eg:System.assertNotEquals(c,(a+b),'Sum Equals');

All Answers

suresh sanneboina 4suresh sanneboina 4
Hi,

integer a =10;
integer b =20;
integer c;
c=a+b;
system.debug('======'+c);
system.assert(c!=(a+b),'Its an Error');
system.assertequals(c,(a-b),'Sum Not Equals');
System.assertNotEquals(c,(a+b),'Sum Equals');

System.assert(condition, msg)
Asserts that the specified condition is true. If it is not, a fatal error is returned that causes code execution to halt.
eg:system.assert(c!=(a+b),'Its an Error');

System.assertEquals(expected, actual, msg)
Asserts that the first two arguments are the same. If they are not, a fatal error is returned that causes code execution to halt.
eg:system.assertequals(c,(a-b),'Sum Not Equals');

System.assertNotEquals(expected, actual, msg)
Asserts that the first two arguments are different. If they are the same, a fatal error is returned that causes code execution to halt.
eg:System.assertNotEquals(c,(a+b),'Sum Equals');
This was selected as the best answer
bonny mankotiabonny mankotia
Thanks Suresh thats a good example.
Rupal KumarRupal Kumar
Hi,
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.
 
public with sharing class AssertEx 
{
    
    public integer sum()
    {
         integer a =10;
            integer b =20;
         integer c;
         c=a+b;
  system.assert(a==10); 
 System.assertEquals(a,b);
 System.assertNotEquals(10,c);
    }
}


Thanks
Rupal Kumar
Mirketa Software Pvt Ltd
http://mirketa.com/index.html
 
bonny mankotiabonny mankotia
Hello Rupal I think 11 line give an error.