Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit testing private method
    primarykey
    data
    text
    <p>First of all, I know there is a lot of questions about this topic. I've read most of them, but I really don't know whether it's lack of proper OO programming, or if I'm missing something.</p> <p>So I have a class <code>xmlRead</code> that reads in an XML file into some lists. I want to unit test this class. I reckon the easiest way to start of is to test <code>addDataToList()</code>. But that's a private method. So I'm wondering if I should make it public, or test the public method ReadTheXmlFile().</p> <pre><code>public class xmlRead { List&lt;string&gt; ... // A couple of lists that need to filled with data from the XML document xmlDocument xDoc = new xmlDocument public void ReadTheXmlFile() { // Find default file, if it doesn't exist, ask user for file through Openfiledialog // and open XMLDocument + error handling if XMLdocument is empty etc. xDoc.load(filepath); takeInXmlData(); } private void takeInXmlData() { addDataToList(list&lt;string&gt; list1, xmlNode 1); // More addDataToList for different lists ... addDataToList(list&lt;string&gt; list2, xmlNode 2); } private void addDataToList(list&lt;string&gt; inputList, xmlNode) { foreach (XmlNode node in xmlDoc.SelectNodes(xmlNode)) { inputList.Add(node.SelectSingleNode("Specific name of node").InnerText); } } </code></pre> <p>So I tried to separate things as much as possible. But this also means my method <code>addDataToList</code> is very small, but easy to unit test. But I also feel it shouldn't be a public method either. I could of course test the public method <code>ReadTheXmlFile()</code>, but then I'd have to make specific test cases for each outcome of error detection, and in my opinion I wouldn't properly test the actual intake of data into the list.</p> <p>Am I just too overprotective and should I make <code>addDateToList</code> (or <code>takeInXmlData</code>) public? Or should I just test the public method <code>ReadTheXmlFile</code> until I've taken into account all the possible ways?</p> <p>It just feels like a lot of work, which sort of goes against the principle of short, simple unit tests.</p> <p>PS: No need to worry about the fact that I load xDoc directly here, I have an Interface that manages the loading of the XML document (which I can stub later to break the dependencies). The focus is on the private methods here.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload