<p>What is the best way of testing a function that throws on failure? Or testing a function that is fairly immune to failure?</p>

<p>For instance; I have a <code>I/O Completion Port</code> class that throws in the constructor if it can't initialise the port correctly. This uses the <code>Win32</code> function of <code>CreateIoCompletionPort</code> in the initialiser list. If the handle isn't set correctly - a non-null value - then the constructor will throw an exception. I have never seen this function fail.</p>

<p>I am pretty certain that this (and other functions like it in my code) if they fail will behave correctly, the code is 50 lines long including white-space, so my questions are</p>

<p>a) is it worth testing that it will throw<br>
b) and if it is worth testing, how to?<br>
c) should simple wrapper classes as these be unit-tested?  </p>

<p>For b) I thought about overriding <code>CreateIoCompletionPort</code> and passing the values through. In the unit test override it and cause it to return 0 when a certain value is passed in. However since this is used in the constructor then this needs to be static. Does this seem valid or not?</p>

[{'answer_id': 7624, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 2, 'selected': False, 'text': '<p>It is definitely worthwhile to test failure conditions, both that your class properly throws an exception when you want it to and that exceptions are handled properly in the class.</p>\n\n<p>This can easily be done if you are acting on an object passed in to the constructor... just pass in a mock.  If not, I tend to prefer to have the functionality moved to a protected method, and override the protected method to evoke my failure case.  I will use Java as an example, but it should be easy enough to port the ideas to a C# case:</p>\n\n<pre><code>public class MyClass {\n    public MyClass() throws MyClassException {\n        // Whatever, including a call to invokeCreateIoCompletionPort\n    }\n\n    protected int invokeCreateIoCompletionPort(String str, int i) {\n        return StaticClass.createIoCompletionPort(str, i);\n    }\n}\n\npublic class MyTest {\n    public void myTest() {\n        try {\n            new MyClass();\n            fail("MyClassException was not thrown!");\n        } catch (MyClassException e) {\n        }\n    }\n\n    private static class MyClassWrapper extends MyClass {\n        @Override\n        protected int invokeCreateIoCompletionPort(String str, int i) {\n            throw new ExpectedException();\n        }\n    }\n}\n</code></pre>\n\n<p>As you can see, it is pretty easy to test whether an exception is being thrown by the constructor or method you are testing, and it is also pretty easy to inject an exception from an external class that can throw an exception.  Sorry I\'m not using your actual method, I just used the name to illustrate how it sounded like you are using it, and how I would test the cases it sounded you wanted to test.</p>\n\n<p>Basically, any API details you expose can usually be tested, and if you want to KNOW that exceptional cases work as they should, you probably will want to test it.</p>\n'}, {'answer_id': 7626, 'author': 'Erik van Brakel', 'author_id': 909, 'author_profile': 'https://Stackoverflow.com/users/909', 'pm_score': 1, 'selected': False, 'text': "<p>You should consider writing your code in such a way that you can mock your I/O completion port. Make an interface/abstract class that exposes the methods you need on the I/O object, and write and test implementation that does things like it's supposed to (and an option to simulate failure perhaps).</p>\n\n<p>AFAIK it's a common practice to mock external resources when unit testing, to minimize dependencies.</p>\n"}, {'answer_id': 7639, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 2, 'selected': False, 'text': '<p>If you are doing this in .NET, there is an ExpectedException attribute that you can add to your test:</p>\n\n<pre><code>[Test, ExpectedException(typeof(SpecificException), "Exception\'s specific message")]\npublic void TestWhichHasException()\n{\n    CallMethodThatThrowsSpecificException();\n}\n</code></pre>\n\n<p>Test will pass if the exception of that type and with the specified message is thrown. The attribute has other overloads including having InnerExceptions, etc.</p>\n'}, {'answer_id': 12477581, 'author': 'EricSchaefer', 'author_id': 8976, 'author_profile': 'https://Stackoverflow.com/users/8976', 'pm_score': 0, 'selected': False, 'text': '<p>Sound like C++ to me. You need a seam to mock out the Win32 functions. E.g. in your class you would create a protected method <code>CreateIoCompletionPort()</code> which calls <code>::CreateIoCompletionPort()</code> and for your test you create a class that derives from you  I/O Completion Port class and overrides <code>CreateIoCompletionPort()</code> to do nothing but return <code>NULL</code>. Your production class is still behaving like it was designed but you are now able to simulate a failure in the <code>CreateIoCompletionPort()</code> function.</p>\n\n<p>This technique is from Michael Feathers book "Working effectively with legacy code".</p>\n'}]
['https://Stackoverflow.com/questions/7614', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/342/']