When you have a simple method like sum (int x, int y), itβs easy to write unit tests. You can verify that the method will correctly sum two integers of the sample, for example, 2 + 3 should return 5, then you will check the same for some "extraordinary" numbers, for example, negative values ββand zero. Each of them should be a separate unit test, since a single unit test should contain a single assert.
What do you do when you have complex I / O? Take, for example, an XML parser. You can have one method syntax (String xml) that receives a string and returns a Dom object. You can write separate tests that will verify the correctness of certain text node, that the attributes are parsed OK, that the child element of the node belongs to the parent element, etc. For this, I can write simple input, for example
<root><child/></root>
which will be used to check the relationship between parents and children between nodes, etc. for the rest of the expectations.
Now take a look at follwing Xml:
<root> <child1 attribute11="attribute 11 value" attribute12="attribute 12 value">Text 1</child1> <child2 attribute21="attribute 21 value" attribute22="attribute 22 value">Text 2</child2> </root>
To check if this method works correctly, I need to check a lot of difficult conditions, for example, this attribute11 and attribute12 belong to element1, that text 1 belongs to child1, etc. I do not want to put a few statements in my unit test. How can i do this?
source share