views:

144

answers:

3

I have the following class in C# which creates an object from a propriatery .DLL that requires a license in a reachable directory at the initialization .

public CplexServices{
    private Cplex _cplex;
    public Cplex Cplex { get { return _cplex; } }
    public CplexServices()
    {
        try
        {
            _cplex = new Cplex();
        }
        catch
        {
            throw new Exception("Path or license file is wrong");
        }
    }
}

"new Cplex()" might fail if the Windows system path is wrong or the license file is not correct. I want to write a test to assert if the correct exception is thrown when the path and/or license file is wrong.

How can I write this test without changing the path or the license file?

+1  A: 

You can't. You'll need to set a false location for the file, and confirm that you get the exception.

But probably this isn't something you'd unit test anyway, as the code determining the fail is not up to you.

I presume you set the location for this file in a config? Then you'll need to just test that that location is correct; but consider that if you have multiple deployments, and different configs, you'll need to run your test against the right environment.

Noon Silk
+1  A: 

You could use

string oldPath = Environment.GetEnvironmentVariable("PATH");
Environment.SetEnvironmentVariable("PATH", "MyBadPath");
// Do tests
Environment.SetEnvironmentVariable("PATH", oldPath);

To test a path problem, put in a bad path. To test the license file, put in a path to a test directory which holds a bad license file.

Vinay Sajip
+1  A: 

What are you testing? I believe that you are testing that the error handling path for the call to new Cplex() is correct.

public CplexServices()
{
    try
    {
        _cplex = new Cplex();
    }
    catch
    {
        throw new Exception("Path or license file is wrong");
    }
}

You are not testng new Cplex() itself, that it throws exceptions when the licence file is missing rather, you are testing that if it throws exceptions you do the right thing. In this case it's so trivially correct that I probably would not care too much. However if this were rather more complex, some serious recovery processing to do for example, then we'd much prefer to be able to test this.

An approach, pass in a factory.

public CplexServices(CplexFactory myFactory)
{
    try
    {
        _cplex = myFactory.makeCplex();
    }
    catch
    {
        throw new Exception("Path or license file is wrong");
    }
}

Now in your tests you can pass a factory which can articficially throw the exception triggering the path you want to test.

This still leaves the question of how do you might test the Cplex() constructor itself. Arguably this is not your problem, the authors should have their own unit tests.

djna
I am not testing if new Cplex() works correctly, just wanted to test if it throws the correct exception when path/license is wrong. When "new Cplex" fails on our job server it fails miserably without any error message. I just added the throw exception code so that next time path is wrong some poor soul wouldn't have to spend an hour to find out that it was failing due to bad path or license file. Then I started to think if there's way to test that correct exception is being thrown. Then you guys came in with good ideas. Thanks...
derdo