How to write junit tests for interfaces?

Contrary to the much-voted-up answer that @dlev gave, it can sometimes be very useful/needful to write a test like you’re suggesting. The public API of a class, as expressed through its interface, is the most important thing to test. That being said, I would use neither of the approaches you mentioned, but a Parameterized test instead, where the parameters are the implementations to be tested:

public class InterfaceTesting {
    public MyInterface myInterface;

    public InterfaceTesting(MyInterface myInterface) {
        this.myInterface = myInterface;

    public final void testMyMethod_True() {

    public final void testMyMethod_False() {

    public static Collection<Object[]> instancesToTest() {
        return Arrays.asList(
                    new Object[]{new MyClass1()},
                    new Object[]{new MyClass2()}

Leave a Comment