English 中文(简体)
Vaadin: • 如何测试与Junnit一起产生的锡尔
原标题:Vaadin: How to test generatedCells with JUnit

在瓦亚丁,你可能知道压倒性生产 单元方法只有在表格需要建立其可见的内容时才能使用。 因此,当我为这几类人撰写《Junnit测试》时,即会引发产生煤炭的方法并对其进行测试。 我如何检验这种想法? 或者,我是否必须使用“全球倡议”测试工具(由于它拥有相当昂贵的许可证),我不想这样做。

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

From my understanding of the question, I don t think you need to have a GUI test tool here.

There s my idea for simple testing :

  1. Create an instance AttributeColumnGenerator.
  2. Create an table.
  3. Add an Item to the table
  4. call generateCell with an columnId and itemId.
  5. Do the appropriate assert on the Component returned by the method.

Here s a snippet of my idea

First my ColumnGenerator who only create a Label with the value of the cell.

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);
iii    

iii

测试方法

    @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()));
    iii

也许它不是最佳解决办法,而是发挥作用。

希望能帮助!

问题。

问题回答

The above approach is enough to test a column generator in isolation. However, this falls short when your column generator has a different behavior each time it is called, or when you need to test the interactions of generated components among themselves. One way to solve this is to override specific methods of Table to fake an attach.

这里是(使用Vadin 7.1进行的测试):

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;
        }
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签