CDIUnit with Custom Scopes (Vaadin)

79 views
Skip to first unread message

Patrick Joos

unread,
Feb 5, 2015, 8:08:43 AM2/5/15
to cdi-...@googlegroups.com
Hey

I'm trying to use CDIUnit with Vaadin. First I searched and found this topic: https://groups.google.com/d/topic/cdi-unit/HGeCyHi92Wc/discussion

I did what was suggested there and managed to get a bit further. However, eventually I was stopped by an error:

java.lang.NullPointerException
    at com
.vaadin.cdi.internal.UIBean.<init>(UIBean.java:42)
    at com
.vaadin.cdi.internal.UIScopedContext.wrapBean(UIScopedContext.java:50)
    at com
.vaadin.cdi.internal.AbstractVaadinContext.get(AbstractVaadinContext.java:156)
    at com
.vaadin.cdi.internal.ContextWrapper.get(ContextWrapper.java:49)
    at org
.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:740)
    at org
.jboss.weld.manager.BeanManagerImpl.getInjectableReference(BeanManagerImpl.java:831)
    at org
.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:92)
    at org
.jboss.weld.util.Beans.injectBoundFields(Beans.java:364)
    at org
.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:375)
    at org
.jboss.weld.injection.producer.DefaultInjector$1.proceed(DefaultInjector.java:71)
    at org
.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48)
    at org
.jboss.weld.injection.producer.DefaultInjector.inject(DefaultInjector.java:68)
    at org
.jboss.weld.injection.producer.ResourceInjector.access$101(ResourceInjector.java:46)
    at org
.jboss.weld.injection.producer.ResourceInjector$1.proceed(ResourceInjector.java:70)
    at org
.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48)
    at org
.jboss.weld.injection.producer.ResourceInjector.inject(ResourceInjector.java:65)
    at org
.jboss.weld.injection.producer.BasicInjectionTarget.inject(BasicInjectionTarget.java:109)
    at org
.jboss.weld.bean.ManagedBean.create(ManagedBean.java:150)
    at org
.jboss.weld.context.AbstractContext.get(AbstractContext.java:96)
    at org
.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:98)
    at org
.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:78)
    at ws1
.data.migration.cdiunittest.CdiUnitTestUITest$Proxy$_$$_WeldClientProxy.testSetTitle(Unknown Source)
    at sun
.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun
.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun
.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java
.lang.reflect.Method.invoke(Method.java:483)
    at org
.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org
.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org
.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org
.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org
.jglue.cdiunit.CdiRunner$2.evaluate(CdiRunner.java:173)
    at org
.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org
.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org
.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org
.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org
.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org
.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org
.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org
.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org
.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org
.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org
.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
    at org
.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
    at org
.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
    at sun
.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun
.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun
.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java
.lang.reflect.Method.invoke(Method.java:483)
    at org
.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
    at org
.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
    at org
.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
    at org
.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
    at org
.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)

I tried to create all necessary objects in @BeforeClass, however I'm currently  a bit out of ideas.

I created a test project which highlights the problem. You can see it here: https://github.com/Rosthouse/VaadinCDIUnitTest
Did anybody have any success with testing Vaadin with CDIUnit? I'd love to use it, since it's so simply (and quick!).

Thanks for any help

Bryn Cooke

unread,
Feb 6, 2015, 4:40:39 AM2/6/15
to cdi-...@googlegroups.com
I'm sorry I don't know anything about Vaadin. 

Bryn

...

Juuso Valli

unread,
Feb 8, 2016, 9:35:50 AM2/8/16
to CDI-Unit
Hey,

I'm one of the devs who worked on the Vaadin CDI plugin and may be able to help. The problem is that the Vaadin CDI plugin expects UI.getCurrent() to return the current UI from a threadlocal variable. This can be set manually prior to your test with UI.setCurrent() but that leads to other problems down the line. Your best bet for unit testing is to replace the contexts provided by the plugin with a something simpler. You can do this by making sure that VaadinExtension is not included in your CdiRunner deployment and instead adding something like this:

public class TestExtension implements Extension {

void afterBeanDiscovery(
@Observes final AfterBeanDiscovery afterBeanDiscovery,
final BeanManager beanManager) {
addContext(beanManager, afterBeanDiscovery, NormalUIScoped.class);
addContext(beanManager, afterBeanDiscovery, UIScoped.class);
addContext(beanManager, afterBeanDiscovery, NormalViewScoped.class);
addContext(beanManager, afterBeanDiscovery, ViewScoped.class);
}

private void addContext(BeanManager beanManager, AfterBeanDiscovery afterBeanDiscovery,
Class<? extends Annotation> scope) {
afterBeanDiscovery.addContext(new TestContext(beanManager) {
@Override
public Class<? extends Annotation> getScope() {
return scope;
}
});
}

   public static abstract class TestContext extends AbstractContext {

  private ContextualStorage contextualStorage;

public TestContext(BeanManager beanManager) {
  super(beanManager);
         contextualStorage = new ContextualStorage(beanManager, false, false);
      }

  @Override
      protected ContextualStorage getContextualStorage(Contextual<?> contextual, boolean createIfNotExist) {
  return contextualStorage;
      }

  @Override
      public boolean isActive() {
         return true;
      }
   }

}


Hope this helps.

-Juuso

...
Reply all
Reply to author
Forward
0 new messages