Search code examples
javajunitlambdajmockit

Lambda expectations in Jmockit


I have a I/F

public interface NumberIF { public Integer getNumber(); }

and when I try to call the following func;

public void func(NumberIF data)

I use the following way with lambda

func(() -> 5)

The problem is that I cannot match the above function with the JMockit expectations...

new Expectations() { { someclass.func(() -> 5); }

For Callable<T> objects, .equals method is always comparing some sort of id number rather than the literal objects stored in them. Is there a workaround for this issue?


Solution

  • JMockit matches arguments in expectation/verifications blocks using the equals method (which by default compares the object ids).

    In your example, the object created by the lambda expression (() -> 5) does not implement the equals method hence the object ids are compared.

    Bottom line: if you really want to compare Callable<T> objects, you either have to implement equals in the concrete implementations or you need to make sure you are using the same instance in your expectations block as in your 'production code' call.