You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by hfe <a...@h41.de> on 2017/05/11 10:34:29 UTC
Re: JSF-Testing with embedded TomEE: Manipulate injected object
Hi,
in my previous solutions I was able to use Mocks just with wrapper classes.
So, I was reading more and more code of TomEE and I found out that I can use
an Observer for directly mocking the injected object.
In a Test I call:
BeanToMock bean = Mockito.mock(BeanToMock.class);
when(bean.callme()).thenReturn("Jedi");
HfeObserver.addProducer(BeanToMock.class, () -> bean);
// start container
// request website with #{beanToMock.callme()} included
And the file /META-INF/org.apache.openejb.extension contains the name of
this Observer:
----------
public class HfeObserver {
public static final Map<Class<?>, HfeSupplier> PRODUCERS = new
HashMap<>();
public static void addProducer(Class<?> clazz, Supplier supplier) {
PRODUCERS.get(clazz).setSupplier(supplier);
}
public void observer(@Observes WebBeansContextBeforeDeploy event) {
event.getContext().getBeanManagerImpl().getNotificationManager().addObserver(new
HfeObserverMethod(), ProcessInjectionTarget.class);
}
private static class HfeObserverMethod implements ObserverMethod,
GenericBeanEvent, Extension {
@Override
public Class<?> getBeanClass() {
return HfeObserverMethod.class;
}
@Override
public Type getObservedType() {
return null;
}
@Override
public Set<Annotation> getObservedQualifiers() {
return Sets.newHashSet(AnyLiteral.INSTANCE);
}
@Override
public Reception getReception() {
return null;
}
@Override
public TransactionPhase getTransactionPhase() {
return null;
}
@Override
public void notify(Object event) {
if (event instanceof GProcessInjectionTarget) {
GProcessInjectionTarget injectionTarget =
(GProcessInjectionTarget) event;
Class<?> clazz =
injectionTarget.getAnnotatedType().getJavaClass();
HfeSupplier supplier = PRODUCERS.get(clazz);
if (supplier == null) {
PRODUCERS.put(clazz, supplier = new HfeSupplier());
}
InjectionTarget target =
injectionTarget.getInjectionTarget();
assert target != null;
injectionTarget.setInjectionTarget(new
HfeInjectionTarget(target, supplier));
}
}
@Override
public Class<?> getBeanClassFor(Class<?> eventClass) {
return null;
}
}
private static class HfeInjectionTarget implements InjectionTarget {
private InjectionTarget target;
private Supplier supplier;
public HfeInjectionTarget(InjectionTarget target, Supplier supplier)
{
assert supplier != null;
this.target = target;
this.supplier = supplier;
}
public void inject(Object instance, CreationalContext ctx) {
target.inject(instance, ctx);
}
public void postConstruct(Object instance) {
target.postConstruct(instance);
}
public void preDestroy(Object instance) {
target.preDestroy(instance);
}
public Object produce(CreationalContext creationalContext) {
return supplier.get() == null ?
target.produce(creationalContext) : supplier.get();
}
public void dispose(Object instance) {
target.dispose(instance);
}
public Set<InjectionPoint> getInjectionPoints() {
return target.getInjectionPoints();
}
}
public static class HfeSupplier implements Supplier {
private Supplier supplier;
public void setSupplier(Supplier supplier) {
this.supplier = supplier;
}
@Override
public Object get() {
return supplier == null ? null : supplier.get();
}
}
}
--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/JSF-Testing-with-embedded-TomEE-Manipulate-injected-object-tp4681575p4681697.html
Sent from the TomEE Users mailing list archive at Nabble.com.