I am doing web automation testing, for example lets say I have two very basic scenarios:
Test a) step 1: add record to the database step 2: check if website displayed record properly
Test b) step 1: edit record on the website step 2: check if record were properly saved in database
By record, let say it was simple text field with some "value"
So for first scenario, I would use Assert equal:
private void check1()
{
Assert.assertEquals(valueFromDB, valueOnWebsite)
//many more assertions for more values here
}
but, for second scenario, it would be:
private void check2()
{
Assert.assertEquals(valueOnWebsite, valueFromDB)
//many more assertions for more values here
}
So basically they both do the same, but are inverted, in order to throw correct error log if assertion was incorrect, now how to make it in single method, that could work for both cases, BUT would display correct assertion log if values were not equal?
Use the overload which receives a message
String message = String.format("valueFromDB value: %s, valueOnWebsite value: %s", valueFromDB, valueOnWebsite);
Assert.assertEquals(message, valueFromDB, valueOnWebsite);
If you want to override the built in message you need to do your own implementation
if (!valueFromDB.equals(valueOnWebsite)) {
throw new AssertionFailedError(String.format("valueFromDB value: %s, valueOnWebsite value: %s", valueFromDB, valueOnWebsite));
}