Unit testing a void method

I understand that you can unit test a void method by checking for its effects. However, looking at the loadConfigFile method in this code:

    internal XmlDocument configData;

    public ConfigFile()
    {
        configData = new XmlDocument();
    }

    /// <summary>
    /// Load config file into memory
    /// </summary>
    /// <param name="filename">path and filename of config file</param>
    public void loadConfigFile(string filename) 
    {
        if(string.IsNullOrEmpty(filename))
            throw new System.ArgumentException("You must specify a filename");

        try 
        {
            configData.Load(filename);
        }
        catch(Exception ex)
        {
            throw new Exception("Config file could not be loaded",ex);
        }
    }

It loads a config file into a private field - which needs to be kept private so that developers do not modify the value directly. Instead the modification will be done through setConfigValue and getConfigValue methods (which I would assume need to be tested separately).

Given this, how would I test that the loadConfigFile actually worked? As I cant access the private configData field.

Jon Skeet
people
quotationmark

You could test that getConfigValue returned the values loaded from the file, basically.

Just because you test setConfigValue/getConfigValue in other tests doesn't mean that you can't use them in the test for loadConfigFile.

As an aside, I'd strongly urge you to follow .NET naming conventions, starting your method names with capital letters. (I'd also urge you to make your field private rather than internal...)

people

See more on this question at Stackoverflow