2012-05-02 36 views
3

在Vaadin中,您可能已经知道overrided generateCell方法仅在Table需要构建其可见内容时调用。所以当我为这个类编写JUnit测试时,我无法触发generateCell方法并对其进行测试。我如何测试这个想法?还是我必须使用一个GUI测试工具forthis(我不想因为它具有相当昂贵的许可证)Vaadin:如何使用JUnit测试generatedCells

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

回答

2

从我这个问题的认识,我不认为你需要有一个GUI测试工具在这里。

有我的简单的测试思路:

  1. 创建一个实例AttributeColumnGenerator。
  2. 创建一个表格。
  3. 将项添加到表
  4. 使用columnId和itemId调用generateCell
  5. 对该方法返回的Component执行适当的断言。

这里是我的想法

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

也许它不是最好的解决办法,但它的作品。

希望它的帮助!

问候。

+0

谢谢你,事情是我在很长的逻辑单元创建我的对象类型,以及我处理此表的数据。当它自然被调用时,会自动调用所有项目和属性。如果我手动拨打电话,我能够仅对该特定项目进行测试吗? – Spring

+0

@Spring是的,你正在测试一个指定项目。您将表格作为generateCell方法的参数,因此您可以访问任何项目。我认为用两个嵌套的foreach可以测试任何具有任何属性的项目。例如:for(Item item:table.getItemsId){for(Object propertyId:table.getContainerPropertyIds()){}} – 2012-05-03 09:33:50

1

上述方法足以单独测试列生成器。但是,当列生成器在每次调用时都有不同的行为,或者需要测试生成的组件之间的相互作用时,这种情况就会不起作用。 解决此问题的一种方法是覆盖表的伪造附加的特定方法。

这里是如何(使用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; 
     } 
    } 
}