使用 JUnit 测试 Tapestry 页面和组件

发布于 2024-09-15 22:07:22 字数 161 浏览 3 评论 0原文

我通常尝试最小化使用 Selenium 进行的测试,并最大化使用普通的旧后端测试(JUnit、mocking)。使用 Tapestry,我发现很难以后一种方式测试页面和组件,因为回调函数会产生“魔力”。

你能解决这个问题吗?或者您只是将 Selenium 用于整个 Web 层(页面、组件)?

I usually try to minimize testing with Selenium and maximize the usage of plain old back-end testing (JUnit, mocking). With Tapestry I am finding it hard to test pages and components in the latter way due to the "magic" that occurs with the callback functions.

Have you been able to solve this? Or are you just using Selenium for the whole web layer (pages, components)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终遇你 2024-09-22 22:07:22

根据 Tapestry 文档,使用 PageTester 是对页面和组件进行单元测试的适当方法: 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 注释元素,您将需要找出其他东西。

According to the Tapestry documentation using PageTester is the appropriate way to do unit testing of Pages and Components: https://tapestry.apache.org/unit-testing-pages-or-components.html

But this seems similar to HtmlUnit style web testing as the interaction happens through a web browser like interface and not through the interface of the Page or Component.

Edit

I just tried a simple unit test for pages and it works quite well :

public class FooPageTest extends AbstractServiceTest{

    @Autobuild
    @Inject
    private FooPage fooPage;

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

}

AbstractServiceTest provides a test runner which provides the Tapestry dependency injection to the unit test class. With Autobuild you get the @Inject dependencies of the FooPage satisfied and for the component injections and @Property annotated elements you will need to figure out something else.

℡寂寞咖啡 2024-09-22 22:07:22

只是为了具体化 Timo 的建议:

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实现(类)

Just to concretise on Timo's suggestion:

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

You will then have properly injected fields in your tests. Remember you @Inject services (interfaces) and you @Autobuild implementations (classes)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文