Make Unit Testing Easier: Quicker Testing of Exceptions in Constructors

0 Flares Twitter 0 Facebook 0 StumbleUpon 0 Google+ 0 LinkedIn 0 0 Flares ×

Over recent months I’ve found that I’ve been doing a lot of repetitive unit testing around ensuring that ArgumentNullExceptions are thrown on constructors. To combat this boiler plate code I wrote a quick test helper which goes through each constructor argument, and tests that they throw the exception:

You use this method within your unit test, passing in each constructor argument (in the correct order), and specifying your target test class as the generic argument:

One of the guys in the office suggested I added an overload which allows you to tell the method to not test certain arguments. This overload is included above, and is called by adding an int array as the first argument that specifies which arguments to ignore:

In this example, the second argument (index of 1) is not tested for ArgumentNullException.

  • Bert Jackson

    It appears that your code snippet is cut off. This looks like a great test helper, but its not all there 🙁 I’m only able to copy out 19 lines of your test class from this page.

    • Hi Bert,

      Sorry about that – something must have got messed up when I added the snippet! I added it now without the missing bits.


The Essential App Marketing Kit
Subscribe To My Newsletter To Get an Entire Chapter From The Book for FREE
Never display this again
0 Flares Twitter 0 Facebook 0 StumbleUpon 0 Google+ 0 LinkedIn 0 0 Flares ×