Should we do unit testing of default methods in interfaces (Java 8)?

I was a bit confused about implementing default methods in interfaces presented in Java 8. I was wondering if I needed to write JUnit tests specifically for the interface and its implemented methods. I tried to do this, but I could not find some recommendations. Please inform.

+5
source share
1 answer

It depends on the complexity of the method. This is really not necessary if the code is trivial, for example:

public interface MyInterface { ObjectProperty<String> ageProperty(); default String getAge() { return ageProperty().getValue(); } } 

If the code is more complex, you should write unit test. For example, this is the default method from Comparator :

 public interface Comparator<T> { ... default Comparator<T> thenComparing(Comparator<? super T> other) { Objects.requireNonNull(other); return (Comparator<T> & Serializable) (c1, c2) -> { int res = compare(c1, c2); return (res != 0) ? res : other.compare(c1, c2); }; } ... } 

How to check it?

Testing a default method from an interface is similar to testing an abstract class.

This has already been answered.

+8
source

Source: https://habr.com/ru/post/1202661/


All Articles