26

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
  • 76,197
  • 13
  • 71
  • 125
willykao
  • 513
  • 2
  • 6
  • 12
  • 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. – Rahul Jul 10 '13 at 20:46
  • @JohnSaunders so in my test, NullReferenceException is when I actually assign the String to the configuration manager. for example... String cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString – willykao Jul 10 '13 at 20:49
  • @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 Jul 10 '13 at 20:50
  • @Rahul I saw that in another solution around here, I'll take a look – willykao Jul 10 '13 at 20:51
  • So either `ConfigurationManager.ConnectionStrings` is null, or `ConfigurationManager.ConnectionStrings["ConnectionString"]` is null. – John Saunders Jul 10 '13 at 20:51

5 Answers5

29

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.

Oleksii Aza
  • 5,368
  • 28
  • 35
  • 2
    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 – willykao Jul 12 '13 at 01:51
3

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.

Rahul
  • 76,197
  • 13
  • 71
  • 125
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);
habakuk
  • 2,712
  • 2
  • 28
  • 47
  • 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. – fizch Apr 27 '18 at 02:58
  • Same here. Six years later I'm hitting the same issue... – Bozhidar Stoyneff Mar 19 '22 at 12:05
1

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

  • This is really a comment, not an answer. Once you gain sufficient reputation, [you will be able to post comments](//stackoverflow.com/privileges/comment). – Mike Zavarello Aug 31 '16 at 18:13
1

first of all you must make sure that you have an app.config file in your nunit tests project.

To add it, you can open the project properties (right click on the project) enter image description here

Enter the details of your connection, it will generate a app.config file or add the right section within :

enter image description here

In your Test class, add the reference to : System.Configuration; => using System.Configuration;

For example you could use your connectionString by this way :

[TestFixture]
public class CommandesDALUnitTest
{

    private string _connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

    [Test]
    public void Method_Test()
    {
        string test = _connectionString;
            ....
    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135