public class Foo<T>
{
public Foo(Bar bar)
{
if (bar == null) throw new ArgumentNullException("bar");
}
}
public class Foo : Foo<Object>
{
public Foo(Bar bar) : base(bar) { }
}
Basically, I understand that I should unit test the constructor for the Generic Foo. Should I also unit test the constructor for the non-generic version? The reason I ask is because the exception is being thrown at the generic constructor level…
[TestMethod]
[ExpectedExeption(typeof(ArgumentNullException))]
public void GenericFooNullArgumentInConstructor()
{
var foo = new Foo<int>(null);
}
//Is this test necessary?
[TestMethod]
[ExpectedExeption(typeof(ArgumentNullException))]
public void NonGenericFooNullArgumentInConstructor()
{
var foo = new Foo(null);
}
In one word: Yes.
The key here is regression – you’d write the unit tests to verify your current implementation (or even before implementation if you’re TDD), but also you write it to protect yourself from breaking changes of the code in the future.
Since both the base and the sub class may be changed in the future – both should be tested.