2012-05-02 35 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; 
     } 
    } 
}