Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8965753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:52:42+00:00 2026-06-15T16:52:42+00:00

I have a asp.net project with a DAL (most code is from someone else

  • 0

I have a asp.net project with a DAL (most code is from someone else but I’m doing maintenance and clean up).

My web application project has a dataprovider class like so:

public class DataProvider : IDataProvider
{
     private string DefaultConnectionString
    {
        get
        {
            return ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["myConnection"]].ConnectionString;
        }
    }

    public T ExecuteMyQuery<T>(parameters)
    {
          //executes query on db
    }

This web application project as has a data layer that interacts between the web pages and the provider like so:

public class MyData : IMyData
{
    private readonly IDataProvider dataProvider;

    public MyData(IDataProvider dataProvider)
    {
        this.dataProvider = dataProvider;
    }

    public string GetTitle(string myVar)
    {
        //builds up stuff to send

         return this.dataProvider.ExecuteMyQuery(...);
     }
 }

The connection string for this project is in web.config.

Now I have my tests. For simplicity and time constraints I’m using just a test database and will create the actual objects and test against the test database (in theory we would use more mocks, stubs and all sorts of stuff but no time.)

So I have a test project which references my web application project above.

public class MyTests
{
    private MyData myData;
    private DataProvider dataProvider;

    [SetUp]
    protected void SetUp()
    {
        dataProvider = new DataProvider();
        mysData = new MyData(dataProvider);   
    }

    [Test]
    public void CheckTitles()
    {
        string title = myData.GetTitle("AVariable");

        Assert.AreEqual(title, "A typical title");
    }

But this keeps give me a null pointer execption when I run it in NUnit. How do I get my provider to use a new connection string to point to my test database? I tried added an app.config file but it’s not working.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:52:43+00:00Added an answer on June 15, 2026 at 4:52 pm

    It seems like your app.config (which is copy of web.config) is not loaded as configuration file by default when tests are being run.

    I’d prefere to solve that in the following way:

    1. Create IConfiguration interface which encapsulates details of getting connectionString

          public interface IConfiguration {
              string MyConnectionString { get; }
          }
      
    2. Create implementation of IConfiguration which reads web.config to use it in real code)

      public class Configuration : IConfiguration
      {
          public string MyConnectionString
          {
              get { return ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["myConnection"]].ConnectionString; }
          }
      }
      
    3. Introduce IConfiguration dependency into DataProvider class

      public class DataProvider : IDataProvider
      {
          private IConfiguration configuration;
      
          public DataProvider(IConfiguration configuration)
          {
              this.configuration = configuration;
          }
      
          private string DefaultConnectionString
          {
              get
              {
                  return configuration.MyConnectionString;
              }
          }
      
          ...
      }
      

      Simple refactoring extracts knowledge about reading web.config from your DataProvider 🙂

    4. In your test use IConfiguration stub which returns any connection string what you need for testing. (you can create stub by using mocking framework like moq/rhino-mock or by implementation of IConfiguration which does not read app.config)

      public class MyTests
      {
          private MyData myData;
          private DataProvider dataProvider;
          private IConfiguration configuration;
      
          [SetUp]
          protected void SetUp()
          {
              // e.g. here stub is created via Moq
              var configurationMock = new Mock<IConfiguration>();
              configurationMock.SetupGet(x => x.MyConnectionString).Returns("test connection string");
              configuration = configurationMock.Object;
      
              dataProvider = new DataProvider(configuration);
              mysData = new MyData(dataProvider);   
          }
      
          ...
      }
      

    If reading web.config is not goal of your tests then that approach is what you need.
    As a bonus knowledge about configuration reading goes to separated class 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET project connecting to a database. A web page sends a
I have an ASP.NET project which has already some custom fields in it's web.config
I have a asp.net project with c# code behind. I have a static class
In my asp.net project currently i have business logic and and data access code
I have an ASP.NET web project and a membership provider configured via my web.config.
Say I have an application with the following structure. // ASP.NET MVC (Web Project)
I have a c# asp.net project where one ascx inherits from a base ascx.
In VS 2010, I have an asp.net web application project. I made a change
I have ASP.Net MVC project. Plus wordpress blog under a subfolder /Blog. Now, when
I have an ASP.NET project that contains many classes. I am thinking about creating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.