Alejandro Alejandro - 2 months ago 18
Java Question

Junit Test generic interface java

I want to know if it is necesary to write a JUNIT test for a generic interface, in this case i have the generic interface:

public interface Operable <T> {

public T suma(T ele1);
public T resta(T ele1);
public T division(T ele1);
public T multiplicacion(T ele1);

And two classes that implements that interface:

public class Complex implements Operable<Complex> {

private final double real, imag;

public Complex(double real, double imag){
this.real = real;
this.imag = imag;


public class Rational implements Operable<Rational>{
private int numerator;
private int denominator;

public Rational(int num, int den){
if (den==0){
throw new IllegalArgumentException();
if (den<0){
numerator = num;
denominator = den;

I did the JUnit tests for the classes but i don't really know if it is necessary to do the test for the interface (it does nothing)

Thank you

Jcs Jcs

No. An interface defines a contract between a caller and a callee. A unit test should verify that an implementation of this interface conforms to this contract.

Note that if you are using java 8 and have "Default Methods" in your interfaces it means that a part of the implementation has been added in the interface. In that case these methods must be unit tested.