System assertEquals Salesforce – System methods

System assertEquals Salesforce

System assertEquals Salesforce

System assertEquals Salesforce, Here I am posting about assert methods in System class. This helps everyone to find the differences between assert methods in salesforce.




Below are the three assert methods in system class.
1. System.assert(condition, msg)
2. System.assertEquals(expected, actual, msg)
3. System.assertNotEquals(expected, actual, msg)

What is System.assert(condition, msg)?

This method asserts that the specified condition is true. If it is not true, a fatal error is returned that causes code execution to halt.



Signature of this method:
Public static void assert(Boolean condition, Object msg)

This method is having two parameters, one is condition which is Boolean type, the other one is msg which is optional and object type.

 What is system.assertEquals(expected, actual, msg)?

This asserts that the first two arguments are the same. if they are not same, a fatal error is returned that causes code execution halt.

Signature of this method:
Public static void assertEquals(Object expected, Objecet actual, object msg)

This method is having three parameters, one is expected which is a type of object, second one is actual which is also type of object and the other one msg which is optional and type of object.

What is system.assertNotEquals(expected, actual, msg)?

This asserts that the first two arguments are not same. if they are the same, a fatal error is returned that causes code execution halt.



Signature of this method:
Public static void assertNotEquals(Object expected, Objecet actual, object msg)

This method is having three parameters, one is expected which is a type of object, second one is actual which is also type of object and the other one msg which is optional and type of object.

Note: We cannot catch an assertion failure using a try/catch block even though it is logged as an exception in case all above three methods.

To know more about this refer below Salesforce documentation URL about salesforce system class.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_system.htm#apex_System_System_methods




Salesforce integration testing – WebServiceMock interface

Salesforce integration testing – WebServiceMock interface

Salesforce integration testing: Generating web service class and how to use those web service classes was explained in my previous post. Here I am going to explain about how to write test class for web services class generated WSDL2Apex.




Every Salesforce developer know about importance of test classes. At least 75% code coverage is required to deploy code to production. Test coverage includes classes generated by WSDL2Apex.

As a Salesforce developer, to write test classes for web service you should spend some time as test methods do not support web service callouts. Apex provides inbuild interface “WebServiceMock” and the “Test.setMock” method. Developers can use this interface to receive fake response in a test method.

What is WebServiceMock interface?



WebServiceMock interface enables sending fake response when testing web service callouts of a class auto-generated from WSDL.

doInvoke is the method of this interface.

doInvoke(stub, soapRequest, responseMap, endpoint, soapAction, requestName, responseNameSpace, responseName, responseType)



The implementation of this method is called by the Apex Runtime to send a fake response when a service callout is made after Test.setMock has been called.

You can refer below Salesforce documentation to know more about this interface.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_interface_webservicemock.htm