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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:29:51+00:00 2026-06-04T00:29:51+00:00

How can i write unit integration tests that talk to a database. e.g.: public

  • 0

How can i write unitintegration tests that talk to a database. e.g.:

public int GetAppLockCount(DbConnection connection)
{
    string query := 
          "SELECT"+CRLF+
          "   tl.resource_type AS ResourceType,"+CRLF+
          "   tl.resource_description AS ResourceName,"+CRLF+
          "   tl.request_session_id AS spid"+CRLF+
          "FROM sys.dm_tran_locks tl"+CRLF+
          "WHERE tl.resource_type = 'APPLICATION'"+CRLF+
          "AND tl.resource_database_id = ("+CRLF+
          "    SELECT dbid"+CRLF+
          "    FROM master.dbo.sysprocesses"+CRLF+
          "    WHERE spid = @@spid)";

    IRecordset rdr = Connection.Execute(query);

    int nCount = 0;
    while not rdr.EOF do
    {
       nCount := nCount+1;
       rdr.Next;
    }

    return nCount;
 }

In this case i am trying to exorcise this code of bugs (the IRecordset returns empty recordset).

[UnitTest]
void TestGetLockCountShouldAlwaysSucceed();
{
   DbConnection conn = GetConnectionForUnit_IMean_IntegrationTest();
   GetAppLockCount(conn);
   CheckTrue(True, "We should reach here, whether there are app locks or not");
}

Now all i need is a way to connect to some database when running a unit integration testing.

Do people store connection strings somewhere for the test-runner to find? A .ini or .xml or .config file?


Note: Language/framework agnostic. The code intentionally contains elements from:

  • C#
  • Delphi
  • ADO.net
  • ADO
  • NUnit
  • DUnit

in order to drive that point home.

  • 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-04T00:29:52+00:00Added an answer on June 4, 2026 at 12:29 am

    Now all i need is a way to connect to some database when running a unit integration testing.

    Either use an existing database or an in-memory database. I’ve tried both an currently use an existing database that is splatted and rebuilt using Liquibase scripts in an ant file.
    Advantages to in-memory – no dependencies on other applications.
    Disadvantages – Not quite as real, can take time to start up.
    Advantages to real database – Can be identical to the real world
    Disadvantages – Requires access to a 3rd party machine. More work setting up a new user (i.e. create new database)

    Do people store connection strings somewhere for the test-runner to find? A .ini or .xml or .config file?

    Yeap. In C# I used a .config file, in java a .props file. With in-memory you can throw this into the version control as it will be the same for each person, with a real database running somewhere it will need to be different for each user.

    You will also need to consider seed data. In Java I’ve used dbUnit in the past. Not the most readable, but works. Now I use a Ruby ActiveRecord task.

    How do you start this? First can you rebuild your database? You need to be able to automate this before going to far down this road.

    Next you should build up a blank local database for your tests. I go with one-per-developer, some other teams share but don’t commit. In a .NET/MS SQL world I think in memory would be quite easy to do.

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

Sidebar

Related Questions

Is it common to write integration tests before writing unit tests? Is it conventional,
We have an old asp.net application that has no unit tests, integration tests, component
In JUnit4 you can write parameterized unit tests by providing parameters collection in one
How can I write a unit test to test the ActualWidth property in a
Can someone please explain how you can write a url pattern and view that
I'm trying to write integration tests to cover a big refactoring of my application
How i can write an unit test, for my Task (sfBaseTask) ?
I have a situation where I need to write some unit tests for some
Here's an integration test I wrote for a class that interacts with a database:
It is usually more complicated to write unit tests due to having to deal

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.