System assertEquals Salesforce
System assertEquals Salesforce, Here I am posting about assert methods in System class. It 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 returned that causes code execution to halt.
Signature of this method:
Public static void assert(Boolean condition, Object msg)
This method has two parameters. One is a condition which is a Boolean type, and the other one is msg which is optional and object type.
What is system.assertEquals(expected, actual, msg)?
Asserts that the first two arguments are the same. If they are not same, a fatal error returned that causes code execution halt.
Signature of this method:
Public static void assertEquals(Object expected, Object actual, object msg)
This method has three parameters, one is expected which is a type of object, the second one is actual, which is also a type of an object and the other one msg which is optional and type of object.
What is system.assertNotEquals(expected, actual, msg)?
This method asserts that the first two arguments are not the same. if they are the same, a fatal error returned that causes code execution halt.
Signature of this method: public static void assertNotEquals(Object expected, Object actual, object msg)
This method has three parameters, first one expected which is a type of object, the second one is actual, which is also the type of object and the other one msg which is optional and a type of object.
Note: We cannot catch an assertion failure using a try/catch block even though it logged as an exception in case all above three methods.
Scenario: Testing Discount Calculation Logic on Opportunities
In this example, the goal is to verify that a custom method, OpportunityService.calculateDiscount
, correctly calculates and applies discounts based on specific conditions. We’ll use System.assert
, System.assertEquals
, and System.assertNotEquals
to validate expected outcomes.
public class OpportunityService {
public static Decimal calculateDiscount(Decimal originalAmount, Integer discountPercentage) {
// Calculates and returns discounted amount
return originalAmount - (originalAmount * (discountPercentage / 100));
}
}
Test Class: Validating Discount Calculations
- System.assert: To ensure that the discount percentage is valid (greater than 0 and less than or equal to 100).
- System.assertEquals: To check if the calculated discount amount matches the expected value.
- System.assertNotEquals: To validate that a wrongly calculated discount is flagged.
@isTest
public class OpportunityServiceTest {
@isTest
static void testCalculateDiscount() {
Decimal originalAmount = 10000;
Integer discountPercentage = 20;
// Calculate expected discount amount
Decimal expectedAmount = 8000; // 20% discount on 10000
// 1. Assert that discount percentage is valid
System.assert(discountPercentage > 0 && discountPercentage <= 100,
'Discount percentage must be between 1 and 100.');
// 2. Assert that calculated amount is as expected
Decimal actualAmount = OpportunityService.calculateDiscount(originalAmount, discountPercentage);
System.assertEquals(expectedAmount, actualAmount,
'Calculated discount amount should be ' + expectedAmount + ' but was ' + actualAmount);
// 3. Assert that a wrong discount amount is flagged
Decimal incorrectExpectedAmount = 8500;
System.assertNotEquals(incorrectExpectedAmount, actualAmount,
'Expected ' + incorrectExpectedAmount + ' does not match the calculated amount.');
}
}
Explanation
System.assert(condition, msg)
: Here,System.assert
ensures that the discount percentage is valid. If the condition fails, it immediately stops the execution, highlighting invalid inputs before further processing.System.assertEquals(expected, actual, msg)
: This assertion checks that the calculated discount (actualAmount
) matches the expected amount (expectedAmount
). If they don’t match, an error message clarifies the discrepancy, helping identify any miscalculation issues.System.assertNotEquals(expected, actual, msg)
: This final assertion confirms that incorrect values are not mistakenly passed as correct. Here, ifactualAmount
were accidentally equal toincorrectExpectedAmount
, the test would fail, emphasizing an unintended calculation match.
This example showcases how System.assert
methods in Salesforce provide robust error-checking mechanisms for validating custom logic, helping teams ensure data integrity and consistency in critical business processes, such as pricing and discount calculations.