How to write junit tests for interfaces?

后端 未结 6 1779
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 03:49

What is the best way to write junit tests for interfaces so they can be used for the concrete implementing classes?

e.g. You have this interface and implementing cla

6条回答
  •  青春惊慌失措
    2020-11-28 04:22

    I strongly disagree with @dlev. Very often it is a very good practice writing tests that use interfaces. Interface defines contract between client and the implementation. Very often all your implementations must pass exactly the same tests. Obviously each implementation can have its own tests.

    So, I know 2 solutions.

    1. Implement abstract test case with various tests that use interface. Declare abstract protected method that returns concrete instance. Now inherit this abstract class as many times as you need for each implementation of your interface and implement the mentioned factory method accordingly. You can add more specific tests here as well.

    2. Use test suites.

提交回复
热议问题