I am in the midst of writing a Test class for my code and there is a snippet of code that passes that I know should be failing.
The code below will always pass:
TestUtilities.SoftAssertEquals(IfCausedException instanceof SOQLUnitOfWork.SOQLUnitOfWorkException, true);
Even though IfCauseException is null - I printed it out with a simple debug statement to verify this). Also, I printed out the check to see what it is returning (the instanceof) and that returns true when it shouldn't.
So the question is, why is this returning true when it shouldn't.
I appreciate anyone's help in advance.
I also tested this by simply printing the value back - i.e.
System.Debug(LoggingLevel.Info, IfCauseException instanceof SOQLUnitOfWork.SOQLUnitOfWorkException)
It still returns true.
For a clearer example:
Exception IfCausedException = null;
System.Debug(LoggingLevel.Info,'\n\n This is it ' +
(IfCausedException instanceof SOQLUnitOfWork.SOQLUnitOfWorkException) + '\n\n');
returns 'This is it true'.
This is my solution (big thanks to Peter on this!)
All I needed was to add the following:
if(!TestUtilities.IsDisabled('Assert should fail')) TestUtilities.SoftAssertEquals(IfCausedException instanceof SOQLUnitOfWork.SOQLUnitOfWorkException && IfCausedException != null, true);
This roughly translates to this:
System.Assert((SOQLUnitOfWork.SOQLUnitOfWorkException && IfCausedException != null) == true)
This is the method you wanted. I didn't post it because all I was worried about was the true/false return on the instanceof keyword. But, here is the method anyway.
Note: This is an altered implementation by Adam (the user here on SE)
return (!isSoftAssert()) ? System.assert(ObjectOne == ObjectTwo) != null:
(ObjectOne == ObjectTwo) ?
System.debug(LoggingLevel.Info, '\n\nSoft Assert Succeeded: [' + ObjectOne + ' = ' + ObjectTwo + ']\n') == null:
System.debug(LoggingLevel.Info, '\n\nSoft Assert Failed: [' + ObjectOne + ' != ' + ObjectTwo + ']\n') != null;