3

您可能知道在 Vaadin 中,仅当 Table 需要构建其可见内容时才调用覆盖的 generateCell 方法。因此,当我为此类编写 JUnit 测试时,我无法触发 generateCell 方法并对其进行测试。我怎样才能测试这个任何想法?还是我必须为此使用 GUI 测试工具(我不想这样做,因为它的许可证非常昂贵)

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

2 回答 2

2

根据我对这个问题的理解,我认为你不需要在这里有一个 GUI 测试工具。

我对简单测试的想法是:

  1. 创建一个实例 AttributeColumnGenerator。
  2. 创建一个表。
  3. 向表中添加项目
  4. generateCell使用 columnId 和 itemId调用。
  5. 对方法返回的组件执行适当的断言。

这是我的想法的一个片段

首先我的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()));
    }

也许这不是最好的解决方案,但它是有效的。

希望有帮助!

问候。

于 2012-05-03T03:31:31.033 回答
1

上述方法足以单独测试列生成器。但是,当您的列生成器每次调用时都具有不同的行为时,或者当您需要测试生成的组件之间的交互时,这就不足为奇了。解决此问题的一种方法是覆盖 Table 的特定方法来伪造附加。

以下是方法(使用 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;
        }
    }
}
于 2014-07-15T18:43:21.457 回答