Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
Assert Class
Namespace
Assert Methods
The following are methods for Assert.
areEqual(expected, actual, msg)
Signature
public static void areEqual(Object expected, Object actual, String msg)
Parameters
- expected: Type: Object Expected value.
- actual: Type: Object Actual value.
- msg: Type: String (Optional) Custom message returned as part of the error message.
Return Value
Type: void
Usage
If the first two arguments aren't the same, a fatal error is returned that causes code execution to halt.
You can’t catch an assertion failure using a try/catch block even though it’s logged as an exception.
Example
1String sub = 'abcde'.substring(2);
2Assert.areEqual('cde', sub, 'Expected characters after first two'); // SucceedsareEqual(expected, actual)
Signature
public static void areEqual(Object expected, Object actual)
Parameters
- expected: Type: Object Expected value.
- actual: Type: Object Actual value.
Return Value
Type: void
Usage
If the two arguments aren't the same, a fatal error is returned that causes code execution to halt.
You can’t catch an assertion failure using a try/catch block even though it’s logged as an exception.
Example
1String sub = 'abcde'.substring(2);
2Assert.areEqual('cde', sub); // SucceedsareNotEqual(notExpected, actual, msg)
Signature
public static void areNotEqual(Object notExpected, Object actual, String msg)
Parameters
- notExpected: Type: Object Value that’s not expected.
- actual: Type: Object Actual value.
- msg: Type: String (Optional) Custom message returned as part of the error message.
Return Value
Type: void
Usage
If the first two arguments are the same, a fatal error is returned that causes code execution to halt.
You can’t catch an assertion failure using a try/catch block even though it’s logged as an exception.
Example
1String sub = 'abcde'.substring(2);
2Assert.areNotEqual('xyz', sub, 'Characters not expected after first two'); // SucceedsareNotEqual(notExpected, actual)
Signature
public static void areNotEqual(Object notExpected, Object actual)
Parameters
- notExpected: Type: Object Value that’s not expected.
- actual: Type: Object Actual value.
Return Value
Type: void
Usage
If the two arguments are the same, a fatal error is returned that causes code execution to halt.
You can’t catch an assertion failure using a try/catch block even though it’s logged as an exception.
Example
1String sub = 'abcde'.substring(2);
2Assert.areNotEqual('xyz', sub); // Succeedsfail(msg)
Signature
public static void fail(String msg)
Parameters
- msg: Type: String (Optional) Custom message returned as part of the error message.
Return Value
Type: void
Usage
Commonly used in a try/catch block test case where an exception is expected to be thrown. You can’t, however, catch the assertion failure in the try/catch block even though it’s logged as an exception.
Example
1// test case where exception is expected
2try {
3 SomeClass.methodUnderTest();
4 Assert.fail('DmlException Expected');
5} catch (DmlException ex) {
6 // Add assertions here about the expected exception
7}fail()
Signature
public static void fail()
Return Value
Type: void
Usage
Commonly used in a try/catch block test case where an exception is expected to be thrown. You can’t, however, catch the assertion failure in the try/catch block even though it’s logged as an exception.
Example
1// test case where exception is expected
2try {
3 SomeClass.methodUnderTest();
4 Assert.fail();
5} catch (DmlException ex) {
6 // Add assertions here about the expected exception
7}isFalse(condition, msg)
Signature
public static void isFalse(Boolean condition, String msg)
Parameters
Return Value
Type: void
Usage
If the condition is true, a fatal error is returned that causes code execution to halt.
You can’t catch an assertion failure using a try/catch block even though it’s logged as an exception.
Example
1Boolean containsCode = 'Salesforce'.contains('code');
2Assert.isFalse(containsCode, 'No code'); // Assertion succeedsisFalse(condition)
Signature
public static void isFalse(Boolean condition)
Parameters
- condition: Type: Boolean Condition you’re checking to determine if it’s false.
Return Value
Type: void
Usage
If the condition is true, a fatal error is returned that causes code execution to halt.
You can’t catch an assertion failure using a try/catch block even though it’s logged as an exception.
Example
1Boolean containsCode = 'Salesforce'.contains('code');
2Assert.isFalse(containsCode); // Assertion succeedsisInstanceOfType(instance, expectedType, msg)
Signature
public static void isInstanceOfType(Object instance, System.Type expectedType, String msg)
Parameters
- instance: Type: Object Instance whose type you're checking.
- expectedType: Type: System.Type Expected type.
- msg: Type: String (Optional) Custom message returned as part of the error message.
Return Value
Type: void
Usage
If the instance isn't of the specified type, a fatal error is returned that causes code execution to halt.
You can’t catch an assertion failure using a try/catch block even though it’s logged as an exception.
Example
1Account o = new Account();
2Assert.isInstanceOfType(o, Account.class); // SucceedsisInstanceOfType(instance, expectedType)
Signature
public static void isInstanceOfType(Object instance, System.Type expectedType)
Parameters
- instance: Type: Object Instance whose type you're checking.
- expectedType: Type: System.Type Expected type.
Return Value
Type: void
Usage
If the instance isn't of the specified type, a fatal error is returned that causes code execution to halt.
You can’t catch an assertion failure using a try/catch block even though it’s logged as an exception.
Example
1Account o = new Account();
2Assert.isInstanceOfType(o, Account.class); // Succeeds1Account o = new Account();
2Assert.isInstanceOfType(o, Account.class, 'Expected type.'); // SucceedsisNotInstanceOfType(instance, notExpectedType, msg)
Signature
public static void isNotInstanceOfType(Object instance, System.Type notExpectedType, String msg)
Parameters
- instance: Type: Object Instance whose type you're checking.
- notExpectedType: Type: System.Type Type that's not expected.
- msg: Type: String (Optional) Custom message returned as part of the error message.
Return Value
Type: void
Usage
If the instance is of the specified type, a fatal error is returned that causes code execution to halt.
You can’t catch an assertion failure using a try/catch block even though it’s logged as an exception.
Example
1Contact con = new Contact();
2Assert.isNotInstanceOfType(con, Account.class, 'Not expected type'); // SucceedsisNotInstanceOfType(instance, notExpectedType)
Signature
public static void isNotInstanceOfType(Object instance, System.Type notExpectedType)
Parameters
- instance: Type: Object Instance whose type you're checking.
- notExpectedType: Type: System.Type Type that's not expected.
Return Value
Type: void
Usage
If the instance is of the specified type, a fatal error is returned that causes code execution to halt.
You can’t catch an assertion failure using a try/catch block even though it’s logged as an exception.
Example
1Contact con = new Contact();
2Assert.isNotInstanceOfType(con, Account.class); // SucceedsisNotNull(value, msg)
Signature
public static void isNotNull(Object value, String msg)
Parameters
- value: Type: Object Value you’re checking to determine if it’s not null.
- msg: Type: String (Optional) Custom message returned as part of the error message.
Return Value
Type: void
Usage
If the value is null, a fatal error is returned that causes code execution to halt.
You can’t catch an assertion failure using a try/catch block even though it’s logged as an exception.
Example
1String myString = 'value';
2Assert.isNotNull(myString, 'myString should not be null'); // SucceedsisNotNull(value)
Signature
public static void isNotNull(Object value)
Parameters
- value: Type: Object Value you’re checking to determine if it’s not null.
Return Value
Type: void
Usage
If the value is null, a fatal error is returned that causes code execution to halt.
You can’t catch an assertion failure using a try/catch block even though it’s logged as an exception.
Example
1String myString = 'value';
2Assert.isNotNull(myString); // SucceedsisNull(value, msg)
Signature
public static void isNull(Object value, String msg)
Parameters
- value: Type: Object Value you’re checking to determine if it’s null.
- msg: Type: String (Optional) Custom message returned as part of the error message.
Return Value
Type: void
Usage
If the value isn't null, a fatal error is returned that causes code execution to halt.
You can’t catch an assertion failure using a try/catch block even though it’s logged as an exception.
Example
1String myString = null;
2Assert.isNull(myString, 'String should be null'); // SucceedsisNull(value)
Signature
public static void isNull(Object value)
Parameters
- value: Type: Object Value you’re checking to determine if it’s null.
Return Value
Type: void
Usage
If the value isn't null, a fatal error is returned that causes code execution to halt.
You can’t catch an assertion failure using a try/catch block even though it’s logged as an exception.
Example
1String myString = null;
2Assert.isNull(myString); // SucceedsisTrue(condition, msg)
Signature
public static void isTrue(Boolean condition, String msg)
Parameters
Return Value
Type: void
Usage
If the specified condition is false, a fatal error is returned that causes code execution to halt.
You can’t catch an assertion failure using a try/catch block even though it’s logged as an exception.
Example
1Boolean containsForce = 'Salesforce'.contains('force');
2Assert.isTrue(containsForce, 'Contains force'); // Assertion succeedsisTrue(condition)
Signature
public static void isTrue(Boolean condition)
Parameters
- condition: Type: Boolean Condition you’re checking to determine if it’s true.
Return Value
Type: void
Usage
If the specified condition is false, a fatal error is returned that causes code execution to halt.
You can’t catch an assertion failure using a try/catch block even though it’s logged as an exception.
Example
1Boolean containsForce = 'Salesforce'.contains('force');
2Assert.isTrue(containsForce); // Assertion succeeds