Question

I'd like to create a mock of this XQPart interface. The problem is that it extends an interface called XQCloneable which has a clone method.

When I in Eclipse create a new class with this set as an interface, I get this class:

public class Part implements XQPart {}

With a red error squiggly under Part saying

CloneNotSupportedException in throws clause of Object.clone() is not compatible with XQCloneable.clone()

What can I do here? Is there no way to make an implementation of this interface?


Note: I did try to implement the method, but didn't realize I could skip the throws declaration as told in accepted answer so kept getting that error.

Was it helpful?

Solution

Your class inherits Object.clone, which is declared to throw CloneNotSupportedException. On the other hand, your class implements XQCloneable, whose clone has no throws clause. If you just create an empty declaration public Object clone() { return null; }, it will make your class compatible with the interface.

OTHER TIPS

If you're making a mock object for unit testing, you need to implement the method (even if it is a no-op). Mocking an interface will require empty methods to match the requirements of the interface. Just make sure the unit you are testing doesn't require the no-op method.

As @Emmerich states, the error happens because XQCloneable extends the Cloneable interface, which is sort of a funny concoction as the clone() method is actually not defined therein, but rather in the Object class!

The semantics is that it shall be possible to create a attribute-by-attribute copy of classes that implement Cloneable and that these classes must @Override the clone() method, as the Object version just throws the CloneNotSupportedException.

Whether your mock and uni test needs to create a copy/clone of the XQPart implementation is up to you to decide/determine - in most cases I would not expect so and just have the method return null or the identity.

Cheers,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top