簡體   English   中英

使用 JUnit 測試 Tapestry 頁面和組件

[英]Testing Tapestry pages and components with JUnit

我通常盡量減少使用 Selenium 的測試,並最大限度地使用普通的舊后端測試(JUnit、模擬)。 使用 Tapestry 我發現很難以后一種方式測試頁面和組件,因為回調函數會產生“魔法”。

你能解決這個問題嗎? 或者您只是將 Selenium 用於整個 web 層(頁面、組件)?

根據使用 PageTester 的 Tapestry 文檔是對頁面和組件進行單元測試的合適方法: https ://tapestry.apache.org/unit-testing-pages-or-components.html

但這似乎類似於 HtmlUnit 風格的 Web 測試,因為交互是通過類似界面的 Web 瀏覽器發生的,而不是通過頁面或組件的界面。

編輯

我剛剛嘗試了一個簡單的頁面單元測試,效果很好:

public class FooPageTest extends AbstractServiceTest{

    @Autobuild
    @Inject
    private FooPage fooPage;

    @Test
    public void setupRender(){
        fooPage.setupRender();
    }

}

AbstractServiceTest 提供了一個測試運行器,它為單元測試類提供 Tapestry 依賴注入。 使用 Autobuild,您可以滿足 FooPage 的 @Inject 依賴項,對於組件注入和 @Property 注釋元素,您需要找出其他內容。

只是為了具體化蒂莫的建議:

public class AbstractServiceTest
{
    @Before
    public void before() throws IllegalAccessException {
        // startupRegistry();
        injectServices();
    }

    private void injectServices() throws IllegalAccessException {
        for(Field field : getClass().getDeclaredFields()) {
            field.setAccessible(true);

            if(field.isAnnotationPresent(Inject.class)) 
                field.set(this, registry.getService(field.getType()));

            if(field.isAnnotationPresent(Autobuild.class))
                field.set(this, registry.autobuild(field.getType()));
        }
    }
}

然后,您將在測試中正確注入字段。 記住你@Inject 服務(接口)和你@Autobuild 實現(類)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM