Cannot Use ConfigurationManager inside Unit Test Project

23,142

Solution 1

It could be one of several issues:

  1. You didn't add app.config to your ProjectTest project.
  2. You didn't add connection string in your app.config.

Solution 2

You are doing a unit test and in unit test your concentration should be the particular method trying to test and should remove extraneous dependencies. in this case, try mocking/moleing(use Microsoft Mole and Pex) system.configuration class; that will give a solution for sure.

What I am saying, once you install MS moles-and-pex -> in your test project solution -> right-click the system assembly and choose create mole.

That will give you a mole'ed version of configuration class which in turn will have a mocked version of configuration class -- using which you can bypass the problem you are facing.

Solution 3

You also can use special configuration paths with the ExeConfigurationFileMap:

// Get the machine.config file.
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
// You may want to map to your own exe.config file here.
fileMap.ExeConfigFilename = @"C:\test\ConfigurationManager.exe.config";
// You can add here LocalUserConfigFilename, MachineConfigFilename and RoamingUserConfigFilename, too
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

Solution 4

It is related to the /noisolation parameter in the command line of mstest.exe. Omitting the /noisolation parameter, it works.

Share:
23,142

Related videos on Youtube

willykao
Author by

willykao

Updated on July 10, 2022

Comments

  • willykao
    willykao almost 2 years

    I'm trying to write a unit test for my project, but it will not let me use the Configuration Manager. Right now my project is set up like

    ASP.Net application (all aspx pages)

    ProjectCore (all C# files - model)

    ProjectTest (all tests)

    in my ProjectCore, I am able to access the ConfigurationManager object from System.Configuration and pass information onto the project. However, when I ran a test where the ConfigurationManager is involved, I get the error

    System.NullReferenceException: Object reference not set to an instance of an object.
    

    Here is an example of the test

    using System.Configuration;
    
    [TestMethod]
    public void TestDatabaseExists()
    {
        //Error when I declare ConfigurationManager
        Assert.IsNotNull(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString
    }
    

    in my other tests, ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString is what I set my data adapter's configuration string to, and returns a null error on the tests but not when I actually use the website. Any ideas?

    • Rahul
      Rahul almost 11 years
      You are doing a unit test and in unit test your concentration should be the particular method trying to test and should remove extraneous dependencies. in this case, try mocking/moleing(use MS Mole/Pex) system.configuration class; that will give a solution for sure.
    • willykao
      willykao almost 11 years
      @JohnSaunders so in my test, NullReferenceException is when I actually assign the String to the configuration manager. for example... String cs = ConfigurationManager.ConnectionStrings["ConnectionString"].C‌​onnectionString
    • willykao
      willykao almost 11 years
      @JohnSaunders unless if ConfigurationManager is null only in the case of a unit test (because I do not get this error in the actual ASP.net application), wondering if I can actually use configurationmanager in tests
    • willykao
      willykao almost 11 years
      @Rahul I saw that in another solution around here, I'll take a look
    • John Saunders
      John Saunders almost 11 years
      So either ConfigurationManager.ConnectionStrings is null, or ConfigurationManager.ConnectionStrings["ConnectionString"] is null.
  • willykao
    willykao almost 11 years
    hahaha oops, I had a web.config file instead of app.config. Reason was I had the web.config in the ASP.net project, and since the core doesn't need the config file it worked there, I thought I could've used the same one but thats noob, thanks for the reminder
  • M. Zavarello
    M. Zavarello over 7 years
    This is really a comment, not an answer. Once you gain sufficient reputation, you will be able to post comments.
  • fizch
    fizch about 6 years
    I just had to use this solution because the configuration file in my integration test project was not getting loaded. It is a class library with xUnit tests and app.config setup. I am using ReSharper in Visual Studio 2017. I can see that it in the bin folder the app.config is renamed properly and I can see that the project is running from that folder. Haven't found any other solutions.
  • Bozhidar Stoyneff
    Bozhidar Stoyneff about 2 years
    Same here. Six years later I'm hitting the same issue...