Question

Hi Assume I have an Interface A and a class B that implements A. Within my test class I create a dummy class that implements A and I "test the Interface methods" now my question is should I test the methods that class B "gets" from the interface.

Was it helpful?

Solution

In my experience, you just test concrete classes and their interaction with interfaces.

That is, if you have concrete class B that implements A, you just test B and its interaction with other objects it references.

OTHER TIPS

Generally testing should touch all (executable) lines of code. If you are implementing an interface it makes it that much easier, since you can code tests that form the "contract" of the interface and now the tests apply to all implementors of the interface.

This ensures consistency across all implementors. Should you encounter a situation where implementors behave differently (e.g. NullReferenceException vs. ArgumentNullException) you can add tests specifying which is "right" and which is wrong. This leads to less surprises down the road.

I might even go as far as saying that every interface should have a set of tests attached to describe the expected behaviour.

There are of course implementation specific things that can only be tested on the concrete implementor (e.g. "Was the file written?" vs. "Was the record comitted?"). These things should be provided through overriding or lambdas to the interface's test suite.

yes, you should aim to get 100% code coverage with your testing

Since your interface shouldn't have any concrete implementation then you don't need to test it since there is nothing to test by definition. The testing should be for the concrete implementation of the interface.

If you find yourself in a situation where you need to have a partial implementaton of an interface you can do what I do. For instance, say I have a interface of an item. This I call IItem and has all the interface. Then I declare an Item which is the partial implementation of the interface for common code and then ItemA, ItemB, etc. for the specialisations of Item.

I read all your posts I I think this solution works best.

Interface A
{
   String A1();
   String A2();
}

public class B:A
{
   String A1(){return "A1"}
   String A2(){return "A2"}
}

public class testB
{
   public void B_Can_Return_A1()
   {
      A b=new B();
      Assert.True(b.A1=="A1")
   }
}

But if you are removing a method from an interface that the concrete implementations still rely on surely you shouldn't be removing that part of the interface?

This is true but this should still be enforced in tests i.e. tested. interfaces (should) play a big role in development and changes may create huge problems down the line. If an object implements an interface I think this is how it should be tested or something similar.

Please comment on this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top