Cannot Use ConfigurationManager inside Unit Test Project
Solution 1
It could be one of several issues:
- You didn't add app.config to your ProjectTest project.
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.
Related videos on Youtube
willykao
Updated on July 10, 2022Comments
-
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 almost 11 yearsYou 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 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"].ConnectionString
-
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 almost 11 years@Rahul I saw that in another solution around here, I'll take a look
-
John Saunders almost 11 yearsSo either
ConfigurationManager.ConnectionStrings
is null, orConfigurationManager.ConnectionStrings["ConnectionString"]
is null.
-
-
willykao almost 11 yearshahaha 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 over 7 yearsThis is really a comment, not an answer. Once you gain sufficient reputation, you will be able to post comments.
-
fizch about 6 yearsI 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 about 2 yearsSame here. Six years later I'm hitting the same issue...