Vaadin: How to Test Generated Cells with JUnit

In Vaadin, as you probably know, the overridden generateCell method is called only when Table needs to create its visible content. Therefore, when I write JUnit Test for this class, I could not run the generateCell method and test it. How can I test these ideas? Or do I need to use a GUI testing tool that I do not want, because it has a rather expensive license.

public class AttributeColumnGenerator implements Table.ColumnGenerator {    
@Override
public Object generateCell(Table source, Object itemId, Object columnId) {
    //lots of code here to be tested
}
}
+3
source share
2 answers

From my understanding of the question, I don’t think you need a GUI testing tool.

There is my idea for simple testing:

  • Create an instance of AttributeColumnGenerator.
  • Create a table.
  • Add item to table
  • generateCell columnId itemId.
  • , .

ColumnGenerator, .

public class AttributeColumnGenerator implements Table.ColumnGenerator {

public Object generateCell(Table source, Object itemId, Object columnId) {

    String textToDisplay  = (String)source.getItem(itemId).getItemProperty(columnId).getValue();
    return new Label(textToDisplay);
}    

}

    @Test
    public void attributeColumnGenratortest()
    {

        AttributeColumnGenerator columnGenerator = new AttributeColumnGenerator();

        Table table = new Table();
        String columnId = "test";
        table.addContainerProperty(columnId, String.class, "");

        String itemId = "item1";
        Item item = table.addItem(itemId);
        item.getItemProperty(columnId).setValue("Value of item1");


        Label generateObject = (Label)columnGenerator.generateCell(table, itemId, columnId);

        // Assert any properties of the returned Component.
        // In this snippet, I only printOut the boolean comparaison.
        System.out.println( "Value of item 1".equals(generateObject.getValue()));
    }

, , .

, !

.

+2

. , . - .

( Vaadin 7.1.13):

package com.table;

import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Table;
import org.junit.Assert;
import org.junit.Test;

/**
 * @author bernard paulus
 * @since 10/07/2014
 */
public class ColumnGeneratorTest {
    @Test
    public void testColumnGenerator() {
        BeanItemContainer<Bean> container = new BeanItemContainer<Bean>(Bean.class);
        container.addBean(new Bean());
        // fake the attach method
        Table testTable = new Table(null, container) {

            private boolean isTableAttached;

            @Override
            public void attach() {
                isTableAttached = true;
                refreshRenderedCells();
            }

            @Override
            public boolean isAttached() {
                return isTableAttached;
            }
        };

        CountingNullGenerator generator = new CountingNullGenerator();
        testTable.addGeneratedColumn(Bean.VALUE, generator);

        // call our fake attach
        testTable.attach();

        Assert.assertEquals(1, generator.getNumberOfCalls()); // check side-effect of generation
    }

    public static class CountingNullGenerator implements Table.ColumnGenerator {
        private int nCalls= 0;

        @Override
        public Object generateCell(Table source, Object itemId, Object columnId) {
            nCalls++;
            return null;
        }

        public int getNumberOfCalls() {
            return nCalls;
        }
    }

    public static class Bean {
        public static final String VALUE = "value";
        private String value;

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }
}
+1
source

All Articles