views:

359

answers:

4

I have a static class representing a connection pool that I want to unit test using Visual Studio 2008's built-in unit testing framework. The static class has some constants in it, like for the maximum allowed connections. I want to decrease this value for my unit test so I don't have to have a lot of connections open in order to hit a test condition (specifically, the code for when the maximum allowed connections has been reached). Is there a way I can edit this constant just in the unit test? That doesn't seem possible to me.

One solution I have is to write another method for accessing the code to be tested, one that would take 'maximum allowed connections' as a parameter. That seems slightly dirty in that it modifies the code I want to test, even if only slightly; however, is there a better alternative? Here's some of my code, for reference:

internal static class ConnectionPool<T> where T : Connection, new()
{
    private const int MAX_OBJECTS = 25;
    private static int _totalConnections;

    internal static T getConnection(bool testMode)
    {
        if (_totalConnections >= MAX_OBJECTS)
        {
            // Here's the code I want to test
        }
    }
}
+2  A: 

No, you can't modify constant, but you can replace it with static readonly field and modify that field using reflection.

Lloyd
A: 

It occurred to me that there are two parts to the condition for the code being tested: the constant MAX_OBJECTS is one of them, true, but there is also _totalConnections. Using an accessor class, which Visual Studio generates for me to have access to private methods and variables within ConnectionPool, I can modify the value of _totalConnections in order to make the condition if (_totalConnections >= MAX_OBJECTS) true. In other words, I'll lie to the static class in my unit test so that I don't have to create MAX_OBJECTS connections in order to meet the condition.

Sarah Vessels
+1  A: 

Before you do this, you should ask yourself about the importance of this limit and if by changing it are you changing nature of the tests? In other words, if I change MAX_OBJECTS to 5, is my code more or less vulnerable to problems that result from resource contention or multithreading issues? Is your avoidance of testing the "real" conditions because it is too slow or too impractical? If so, maybe it's best to treat this as a different class of unit test. Where I work, we have a number of tests that are vital but costly in terms of time. If left in at all times, they would make continuous integration impractical, therefore we created the NUnit category "Nightly" and tests that take longer than a minute must bear the Category attribute with Nightly.

plinth
"Is your avoidance of testing the "real" conditions because it is too slow or too impractical?" Yes. Good suggestion about separating unit tests into nightly v. anytime.
Sarah Vessels
+1  A: 

You can get it from a configuration file, so you can have different values for testing.

Samuel Carrijo