In a unit test, is it generally a good idea to test returned values according to the string their toString() returns?
For example, doing the following to make sure the expected list is returned:
assertEquals(someExpression.toString() ,"[a, b, c]");
The only time that I would test on
toString() of an object is when I have an uneditable class that did not implement
equals, but instead implemented a
toString() to output its field's content. Even then, I won't use a hardcoded string as the equality test, but instead do something like
SomeObject b = new SomeObject(expected, values, here); assertEquals(a.toString(), b.toString());
Your approach might save time initially, but in the long run it would take far more time just to maintain the test since you are hardcoding the string of the expected result of the
Edit 1: Naturally, if you are testing a function / process that outputs a string, that would be one of the times you should be using a hardcoded string as the expected result.
String input = "abcde"; String result = removeVowels(input); assertEquals(result, "bcd");