-
1. Re: Mocking interceptor collaborators
marcio.dantas Jul 6, 2013 5:30 PM (in response to marcio.dantas)I was able to wrap the interceptor's injection target using the ProcessBean lifecycle event as below.
But don't know if it's the best way and still would like some recommendations on this. Cheers.
public class EasymockCdiExtension implements Extension {
...
/**
* Processes enabled interceptors.
* @param event enabled bean event
* @param manager bean mnager
*/
public void processEnabledInterceptor(
@Observes final ProcessBean<Object> event) {
final Bean<Object> bean = event.getBean();
if (bean instanceof Interceptor) {
final Interceptor<Object> interceptor = (Interceptor<Object>) bean;
wrapInterceptorInjectionTarget(interceptor);
}
}
/**
* Wraps interceptor injection target.
* @param interceptor interceptor
*/
private void wrapInterceptorInjectionTarget(
final Interceptor<Object> interceptor) {
try {
@SuppressWarnings("unchecked")
final InjectionTarget<Object> injectionTarget =
Whitebox.getInternalState(interceptor,InjectionTarget.class);
Whitebox.setInternalState(interceptor,
new TestSubjectInjectionTarget<Object>(injectionTarget));
} catch (final RuntimeException e) {
System.err.println("[WARN] " + getClass().getName()
+ ": Couldn't wrap interceptor "
+ interceptor.getBeanClass() + ".");
}
}
...
}