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?
What are you testing? I believe that you are testing that the error handling path for the call to new Cplex() is correct.
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.
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.