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 7938979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:57:37+00:00 2026-06-03T22:57:37+00:00

A seemingly simple one here. How can I test the exception handling of this

  • 0

A seemingly simple one here. How can I test the exception handling of this code block using a unit test?

    public DbFactoryResponseType Close()
    {
        DbFactoryResponseType dbFactoryResponse = new DbFactoryResponseType();
        try
        {
            if (m_isConnected)
            {
                m_isConnected = false;

                if (m_hasRecordSet)
                {
                    m_hasRecordSet = false;

                    m_dbFactoryDatabaseDataReader.Close();
                    m_dbFactoryDatabaseDataReader.Dispose();
                }

                m_dbFactoryDatabaseCommand.Dispose();
                m_dbFactoryDatabaseConnection.Close();
                m_dbFactoryDatabaseConnection.Dispose();

            }

            dbFactoryResponse.ExceptionMessage = "";
            dbFactoryResponse.Success = true;
            dbFactoryResponse.UserFriendlyMessage = "OK";

            return dbFactoryResponse;
        }
        catch (Exception ex)
        {
            dbFactoryResponse.ExceptionMessage = ex.Message;
            dbFactoryResponse.Success = false;
            dbFactoryResponse.UserFriendlyMessage = "Error: Error while attempting to close the database connection.";

            return dbFactoryResponse;
        }
    }

Here is what I have so far but I do not know how to make the exception fire allowing me to test the output.

    /// <summary>
    /// Test method to test closing a PosgreSQL database connection.
    /// </summary>
    [TestMethod]
    public void TestClosePostgreSQLConnectionException()
    {
        const string connectionString = "Server=myIp;Port=myPort;Database=myDatabase;User Id=myUser;Password=myPassword;";

        const string provider = "Npgsql";

        DbProviderFactoryConnection aDbProviderFactoryConnection = new DbProviderFactoryConnection(connectionString, provider);

        DbFactoryResponseType dbFactoryResponseType = aDbProviderFactoryConnection.Close();

        Assert.IsNotNull(dbFactoryResponseType);

        Assert.AreEqual(false, dbFactoryResponseType.Success);
    }  
  • 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-03T22:57:38+00:00Added an answer on June 3, 2026 at 10:57 pm

    You can create a mock version of IDbFactoryDatabaseConnection (which is an interface that you will need to introduce), then in the setup of your mock, throw an exception when calling Close() on the mock and then in your test check ExceptionMessage, Success and UserFriendlyMessage.

    The way you do this is to use a mocking framework like Rhino Mocks or MoQ or you could even create a mock or stub of your own. You would then, in your test inject the mock version of the class into your class constructor (assuming you are using constructor injection rather than setter injection) and then you’ll be able to control the behaviour of your mock.

    An example, using MoQ, of how you would do this is below:

    Mock<IDbFactoryDatabaseConnection> connectionMock = new Mock<IDbFactoryDatabaseConnection>();
    
    DbProviderFactoryConnection aDbProviderFactoryConnection = new DbProviderFactoryConnection(connectionString, provider, connectionMock.Object);
    
    connectionMock.Setup(c => c.Close()).Throws<Exception>();
    
    DbFactoryResponseType dbFactoryResponseType = aDbProviderFactoryConnection.Close();
    

    Of course, in line with industry best practice this also means that you must adhere to SOLID principles, specifically the principle of Dependency Inversion, which means that you will need to create an interface for the DbFactoryDatabaseConnection class (I assume that is the name of your class), which is what I’ve shown in the example above.

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

Sidebar

Related Questions

I am trying to run this seemingly simple piece of code which is repeated
So basically my problem is a seemingly simple one. You can see it in
After doing a quick search I can't find the answer to this seemingly simple
I can't make this seemingly simple call on my DomainService compile. I keep getting
This is a seemingly simple problem but I am having trouble doing it in
I have a seemingly simple problem, but I can't quite figure out a solution.
I'm having difficulty doing this seemingly simple task. I want to load XML files
In OpenGL, one often writes code like this: glPushMatrix(); // modify the current matrix
I have a seemingly simple requirement, but i can't figure out how to write
I'm using Entity Framework 4.1 and have a seemingly simple requirement: I want to

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.