ConversionException in tests: Unable to convert value of type Integer

I get this while trying to run a junit test with Vaadin 7:

com.vaadin.data.Buffered$SourceException
	at com.vaadin.ui.AbstractField.setPropertyDataSource(AbstractField.java:634)
	at com.vaadin.ui.Table.bindPropertyToField(Table.java:3756)
	at com.vaadin.ui.Table.getPropertyValue(Table.java:3725)
	at com.vaadin.ui.Table.parseItemIdToCells(Table.java:2104)
	at com.vaadin.ui.Table.getVisibleCellsNoCache(Table.java:1986)
	at com.vaadin.ui.Table.refreshRenderedCells(Table.java:1641)
	at com.vaadin.ui.Table.refreshRowCache(Table.java:2394)
	at com.vaadin.ui.Table.setPageLength(Table.java:1000)
	at com.taskadapter.web.configeditor.PriorityPanel$1.containerItemSetChange(PriorityPanel.java:94)
	at com.taskadapter.web.configeditor.PriorityPanel.buildUI(PriorityPanel.java:99)
	at com.taskadapter.web.configeditor.PriorityPanel.<init>(PriorityPanel.java:66)
	at com.taskadapter.connector.redmine.editor.RedmineEditorFactory.getMiniPanelContents(RedmineEditorFactory.java:73)
	at com.taskadapter.connector.redmine.editor.RedmineEditorFactoryTest.miniPanelIsCreated(RedmineEditorFactoryTest.java:12)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	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.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
	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.runners.ParentRunner.run(ParentRunner.java:300)
	at org.junit.runners.Suite.runChild(Suite.java:128)
	at org.junit.runners.Suite.runChild(Suite.java:24)
	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.runners.ParentRunner.run(ParentRunner.java:300)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
Caused by: com.vaadin.data.util.converter.Converter$ConversionException: 
Unable to convert value of type java.lang.Integer to presentation type class java.lang.String. 
No converter is set and the types are not compatible.
	at com.vaadin.data.util.converter.ConverterUtil.convertFromModel(ConverterUtil.java:98)
	at com.vaadin.ui.AbstractField.convertFromModel(AbstractField.java:705)
	at com.vaadin.ui.AbstractField.convertFromModel(AbstractField.java:690)
	at com.vaadin.ui.AbstractField.setPropertyDataSource(AbstractField.java:626)
	... 43 more

the test itself is pretty simple. it creates an instance of some editor just like the production code would do:

public class RedmineEditorFactoryTest extends FileBasedTest {
    @Test
    public void miniPanelIsCreated() {
        RedmineEditorFactory factory = new RedmineEditorFactory();
        factory.getMiniPanelContents(new Sandbox(true, tempFolder), new RedmineConfig());
    }

at the same time
the server starts and shows the tables just fine
. my
guess
is that Vaadin startup code initializes some UI object with those converters and it does not happen in unit tests (ugh!).

I wonder if the problem lies in Vaadin’s ConverterUtil class, where “session” is always NULL when running unit tests:

   public static <PRESENTATIONTYPE, MODELTYPE> Converter<PRESENTATIONTYPE, MODELTYPE> getConverter(
            Class<PRESENTATIONTYPE> presentationType,
            Class<MODELTYPE> modelType, VaadinSession session) {
        Converter<PRESENTATIONTYPE, MODELTYPE> converter = null;
        if (session == null) {
            session = VaadinSession.getCurrent();
        }

        if (session != null) {
            ConverterFactory factory = session.getConverterFactory();
            converter = factory.createConverter(presentationType, modelType);
        }
        return converter;

    }

we have found a workaround for this issue: need to explicitly set the Vaadin session in the unit test.
I hope there’s a better way than this hack…

public class RedmineEditorFactoryTest extends FileBasedTest {
    @Test
    public void miniPanelIsCreated() {
        VaadinSession.setCurrent(new VaadinSession(new VaadinServletService(
                new VaadinServlet(), new DefaultDeploymentConfiguration(
                        getClass(), new Properties()))));
        RedmineEditorFactory factory = new RedmineEditorFactory();
        factory.getMiniPanelContents(new Sandbox(true, tempFolder),
                new RedmineConfig());
    }
}

that solved my problem, thanks

Please remember to set the VaadinSession instance to a ThreadLocal or to some variable: VaadinSession.setCurrent() uses weak references and GC may be triggered at some point. I got burnt by this.

you sir are genius!