In this post, table manners, I delibrately gave an incomplete test, just to show the way to read a table, now, I am going to talk another important topic, where to assert?
To organize assertion into a cohesive group and output meaningful difference message for debugging purpose, I defined a TableContents class to measure the actual table and put the differences inside a SetDiff class, it means the difference of two sets.
We need to add hashCode method in the Person class since it is used in a HashSet now,
SetDiff class uses Set Operation to find out what elements are only in expectation and what are only in actual results,
S1 - expectation, S2 - actual
only in expectation = S1 - S2
only in actual = S2 - S1
The test is complete.
By splitting responsibilities among different classes, the test becomes very clean, and the error messages can show what's the difference in expectation and actual results. For this test, let us pretend last time, there was only one row in the table,
So the expectation used to be,
Now you are making changes and added 3 rows of data, and changed first row from 49 to 50, when you run the test, here is the result,
It tells you what's the actual result, what is in expectation but doesn't show in actual result and the unexpected result in actual, best thing yet, you can just copy the actual result into the expectation after you verify it is the right result.
And test passes,
However, there is no assertion method in junit which takes Object as parameter, I wrote one by myself, but I am asking Kent Beck to add this into future release of junit.
No comments:
Post a Comment