To catch exceptions (even on constructors) in my tests elegantly I found the following solution in a blog and used it in all my projects on eclipse:
Throwable t= ThrowableCaptor.captureThrowable(() -> (new LinkedList<Object>()));
The ThrowableCaptor is implemented by a functional interface and some small extra code:
public class ThrowableCaptor {
@FunctionalInterface
public interface Actor {
void act() throws Throwable;
}
public static Throwable captureThrowable(final Actor actor) {
Throwable result = null;
try {
actor.act();
}
catch (final Throwable throwable) {
result = throwable;
}
return result;
}
}
Now, as I said, in Eclipse the thing worked like a charm. However under Netbeans I always get the following error message:
incompatible types: bad return type in lambda expression
LinkedList<Object> cannot be converted to void
Well, I have no clue, what is wrong...
Update 1 After some research I stumbled upon this:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=398734
I am not sure if it is related, but to my understanding, it suggests a problem with eclipse. Thus my code should indeed be the problem.
I assume, that the problem is matching new LinkedList<Object>
to void act() throws Throwable
. I"ll try generics as a solution.
Update 2 No Generics in Functional Interfaces...