The System.assertEquals and System.assertNotEquals methods follow the convention of assertEquals(expected,
actual), where the first argument is the expected value and the second is the actual value being tested.
When these arguments are reversed, the test logic remains correct - it will still pass or fail appropriately. However, the error messages become
confusing and misleading when the test fails.
For example, if you write System.assertEquals(myList.size(), 0) and the list actually contains 4 elements, the failure message will
show: Expected: 4, Actual: 0
This is backwards from what you intended to test. You expected 0 elements but got 4, so the message should read Expected: 0, Actual:
4.
This confusion makes debugging more difficult, especially when:
- Multiple developers work on the same codebase
- Tests fail in CI/CD pipelines where quick understanding is crucial
- Complex test scenarios where clear error messages are essential for troubleshooting
Following the correct argument order makes your tests more maintainable and debugging more straightforward.
What is the potential impact?
When test assertions fail, the error messages will display confusing information that makes debugging more difficult and time-consuming. This can
slow down development and increase the likelihood of misunderstanding test failures.