Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 574 characters in body
Source Link
omnomnom
  • 9.2k
  • 4
  • 43
  • 52

I always test implementations - one class can implement several interfaces and also one interface can be implemented by several classes - each of them should be covered by tests.

The requirement of invoking setter in the unit test (casting the interface to the implementation):

((HelloInterfaceImpl)hi).setTarget(target); 

means that you actually test the implementation. It's not the part of the contract, but this is the important part making the implementation to work and should be tested properly.

Let's take an example from JDK. You have interface List and two implementations: ArrayList and LinkedList. Additionally LinkedList implements Deque interface. If you write test for List interface what would you cover? Array or linked list? What's more in case of LinkedList, what interface would you choose to test? Deque or List? As you see, when you test implementations you don't have such problems.

For me, personally, casting interface to implementation in the unit test is obvious sign that something is going wrong ;)

I always test implementations - one class can implement several interfaces and also one interface can be implemented by several classes - each of them should be covered by tests.

The requirement of invoking setter in the unit test (casting the interface to the implementation):

((HelloInterfaceImpl)hi).setTarget(target); 

means that you actually test the implementation. It's not the part of the contract, but this is the important part making the implementation to work.

I always test implementations - one class can implement several interfaces and also one interface can be implemented by several classes - each of them should be covered by tests.

The requirement of invoking setter in the unit test (casting the interface to the implementation):

((HelloInterfaceImpl)hi).setTarget(target); 

means that you actually test the implementation. It's not the part of the contract, but this is the important part making the implementation to work and should be tested properly.

Let's take an example from JDK. You have interface List and two implementations: ArrayList and LinkedList. Additionally LinkedList implements Deque interface. If you write test for List interface what would you cover? Array or linked list? What's more in case of LinkedList, what interface would you choose to test? Deque or List? As you see, when you test implementations you don't have such problems.

For me, personally, casting interface to implementation in the unit test is obvious sign that something is going wrong ;)

Source Link
omnomnom
  • 9.2k
  • 4
  • 43
  • 52

I always test implementations - one class can implement several interfaces and also one interface can be implemented by several classes - each of them should be covered by tests.

The requirement of invoking setter in the unit test (casting the interface to the implementation):

((HelloInterfaceImpl)hi).setTarget(target); 

means that you actually test the implementation. It's not the part of the contract, but this is the important part making the implementation to work.