EDIT My previous post wasn't helpful at all, here's my second attempt. While you may still want to perform some elements of mocking in this, without your code to look at, I don't know which elements of this approach will work and which ones won't. Anyways, here's an approach I take when trying to test catch blocks or exceptions in general:
public class A
{
public void ExecuteDB2Query(string query, DB2Connection connection)
{
try
{
// Code for executing the DB2 query
}
catch(DB2Exception ex)
{
// Catch block you are trying to test
}
}
// Actual method
public void MyMethod()
{
var query = "Select * FROM TableInApplication";
var connection = new DB2Connection("DATABASE=GOODDB");
ExecuteDB2Query(query, connection);
}
}
[Test]
public void MyMethod_CallsExecutDB2Query()
{
// Test that MyMethod is calling ExecuteDB2Query
}
// Intentionally pass it bad data to test your catch block
// under the notion that the code that executes in the application
// will be using the same method and thus the same catch block
[Test]
public void ExecuteDB2Query_Handles_Exception()
{
var queryString = "Select* FROM NonExistentTable";
var connection = new DB2Connection("DATABASE=BADDB");
var aClass = new A();
Assert.Throws<DB2Exception>(() => aClass.ExecuteDB2Query(queryString, connection));
}
This isn't the most elegant way to handle this but if you can't stub out the exception then splitting up the responsibilities of the code and testing each piece separately by triggering the exception is the next best thing I guess.