Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i test an interface?

I want to do a JUnit test case for an interface, as you know i can't make an object from the interface and i don't want any classes names to show up in that test, i want only to test the interface and only use it's methods in the test.

So, i can't instantiate an object like :

Interface obj = new Class(); 

as i don't wont to use any classes methods, and i don't want to instantiate the interface as :

Interface var = new Interface{//methods};

as i don't want to override the methods in the test.

UPDATE:

i have an interface and a class which implements it:
public interface inter {
public void method1();
public void method2();
}


public class BlaBla implements inter{
@override
public void method1(){//stuff}
@override
public void method2(){//stuff}
}

i want to test BlaBla but deal in my test with it's interface (inter).

like image 732
Muhammed Refaat Avatar asked Mar 21 '26 18:03

Muhammed Refaat


1 Answers

An interface is a contract. It has no logic to test.

You can use a mocking framework like Mockito to create an instance of the interface without having to manually stub the methods. But it will just stub them in the background.

You have to ask yourself what you want to test? Given that the interface methods have no implementation there is no code to test.

Say I have an interface like so

public interface DoesStuff {
    void doStuff();
}

What is there to test? The only thing I have said is that I want a class that implements DoesStuff to doStuff.

like image 159
Boris the Spider Avatar answered Mar 24 '26 08:03

Boris the Spider