Search code examples
javajunitcdiweldneedle4j

JUnit test AroundInvoke Interceptor


class AbstractXYZClass{
 ...
 @AroundInvoke
 public Object intercept(InvocationContext ctx) ... {
   log("do intercepting");
   ctx.proceed();
 }
 ...
}

@Stateless
class XYZClass extends AbstractXYZClass{
 ...
 public void iWantToTestThisMethod() {...}
 ...
}

(running this on Server the interception for iWantToTestThisMethod() works fine)

In my unit tests (using Needle ...@ObjectUnderTest(implementation=XYZClass.class)...) @AroundInvoke doesn't get invoked

How can I JUnit-test XYZClass::iWantToTestThisMethod with intercept() intercepting?


Solution

  • Needle4j is a dependency injection "simulator", it does not support advanced lifecyles and scopes. So you will have to come up with a different testing strategy. I would stick to needle4j when it comes to injection and verification of correct method interactions but switch to something like CDI-Unit or Arquillian for the actual framework-behavior tests.