Hi all!
My first question, apologies in advance if answer is clear, but I am beginning with APEX, and I am a bit stuck here.
What does "System.Assert(false);" is doing exactly here?
try{
ContactsDML.databaseMethodDML();
System.Assert(false);
} catch(DMLException e){
System.assert(e.getMessage().contains('Required fields are missing: [LastName]: [LastName]'));
}
I am within a class method, calling another class for the test, which results in an error (as stated in the message).
My question is because I do not see any changes, whether I use true or false on that parameter: System.Assert(false);Thanks in advance!Hi Anudeep, I double-checked with other sources and indeed this kind of assessment is used to ensure an error is given so you can catch the exception. That is the purpose. Thanks! Did you try using System.Assert(false) in the try statement? What was the result You could give it a try. The documentation says you can’t catch an assertion failure using a try/catch block even though it is logged as an exception though Hi Anudeep!Thanks, I have a second thoughtWhat about "System.Assert(false)" is within the try statement to ensure force error Hi Anudeep, Thanks for the explanation, I also reviewed the documentationMy specific question is what that piece of code ( System.Assert(false); is doing on the whole picture, why it is there because I do not see any changes wether I set it to true to false.So, by your answer, I understand that it is not doing anything, and the code should be like this instead
Did I understand you right?try{
ContactsDML.databaseMethodDML();
// System.Assert(false);
} catch(DMLException e){
System.assert(e.getMessage().contains('Required fields are missing: [LastName]: [LastName]'));
}
As per the documentation, System.Assert asserts that the specified condition is true. If it is not, a fatal error is returned that causes code execution to halt. You should ideally only provide a condition to a system. assert
System.Assert accepts two parameters, one (mandatory) which is the condition to test for and the other a message (optional) to display should that condition be false.System.AssertEquals and System.AssertNotEquals both accepts three parameters; the first two (mandatory) are the variables that will be tested for in/equality and the third (optional) is the message to display if the assert results in false.Now, testing best practices state that you should assert some condition but also output a “console” message that shows the values of each of the operands. This certainly helps you debug unit tests when asserts are failingSystem.assert(condition, msg);
Here is an example showing proper usage
Let me know if this helps, if it does, please close the query by marking it as solved. It may help others in the community. Thank You!@isTest
public class SampleTestClass {
@isTest
static void example(){
List<Account> accountsToBeCreated = new List<Account>();
//Create test data
for(Integer i = 0; i<10; i++){
Account newAccount = new Account();
newAccount.name='test'+i;
newAccount.Industry = 'Energy';
accountsToBeCreated.add(newAccount);
}
//Insert test Data
insert accountsToBeCreated;
//Query test data that was just created
List<Account> createdAccounts = [SELECT Id, Industry From Account];
//Assert list is not empty
System.assert(!createdAccounts.isEmpty(),'List should not be empty');
//Ensure 10 records were created
System.assert(createdAccounts.size() == 10,'10 account records should be returned');
//Loop through accounts ensure industry is not null
for(Account acc : createdAccounts){
System.assert(acc.Industry != null,'Industry should not be null');
}
}
}