01-30-2013 05:38 AM
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
<alternatives>
<class>de.data.experts.activiti.mock.DominoProducerMock</class>
<class>de.data.experts.activiti.mock.PraxisberichtVeroeffentlichenMock</class>
<class>de.data.experts.activiti.mock.ThemaVeroeffentlichenMock</class>
</alternatives>
</beans>
<serviceTask activiti:expression="#{themaVeroeffentlichen.anlegen()}" completionQuantity="1" id="_30" implementation="##WebService" isForCompensation="false" name="Praxisberichtsthema veröffentlichen" startQuantity="1">
<incoming>_31</incoming>
<outgoing>_36</outgoing>
</serviceTask>
The real class is annotated like this:
@Named("themaVeroeffentlichen")
@Dependent
public class ThemaVeroeffentlichen {
And the mock like this:
@Alternative
@Named("themaVeroeffentlichen")
@Dependent
public class ThemaVeroeffentlichenMock extends ThemaVeroeffentlichen {
public Object getValue(ELContext context, Object base, Object property) {
try {
Object result = getWrappedResolver().getValue(this.context, base, property);
context.setPropertyResolved(result != null);
return result;
} catch (IllegalStateException e) {
// dependent scoped / EJBs
Object result = ProgrammaticBeanLookup.lookup(property.toString(), getBeanManager());
context.setPropertyResolved(result != null);
return result;
}
}
public static Object lookup(String name, BeanManager bm) {
Iterator<Bean< ? >> iter = bm.getBeans(name).iterator();
if (!iter.hasNext()) {
throw new IllegalStateException("CDI BeanManager cannot find an instance of requested type '" + name + "'");
}
Bean bean = iter.next();
CreationalContext ctx = bm.createCreationalContext(bean);
// select one beantype randomly. A bean has a non-empty set of beantypes.
Type type = (Type) bean.getTypes().iterator().next();
return bm.getReference(bean, type, ctx);
}
And that's why my test sometimes passes and sometimes not. It depends on the order of the beans in the set. Sometimes the mock is first and sometimes the real class.01-30-2013 07:13 AM
01-30-2013 08:53 AM
@Named("praxisberichtVeroeffentlichen")
@Dependent
public class PraxisberichtVeroeffentlichen {
@Inject
private PraxisberichtVeroeffentlichenDelegate delegate;
public void veroeffentlichen(String student, String thema) {
delegate.veroeffentlichen(student, thema);
}
}
and the delegate is being replaced by a mock durung JUnit-tests.01-30-2013 08:55 AM
01-30-2013 09:00 AM
01-30-2013 03:31 PM
Exactly. Activiti's CDIResolver get's one bean from the ProgrammaticBeanLookup and that bean has been initialized by CDI. That way, the alternative is taken, when running the JUnit-tests.It is an option… An other option might be to have the resolver look at the @Alternative annotation and use that one instead of the normal bean. That requires a change to the code though… Not even sure it works….
At least, that's how I understand all this CDI magic
01-31-2013 01:06 AM
An other option might be to have the resolver look at the @Alternative annotation…For me, it doesn't seem right to try to implement something the CDI framework should already be able to provide, like privilige alternatives.
01-31-2013 04:37 AM
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.