Java 使用JUnit测试Tapestry页面和组件

Java 使用JUnit测试Tapestry页面和组件,java,testing,junit,tapestry,Java,Testing,Junit,Tapestry,我通常会尽量减少使用Selenium进行测试,并最大限度地利用普通的后端测试(JUnit、mocking)。对于Tapestry,由于回调函数的“魔力”,我发现很难用后一种方式测试页面和组件 你能解决这个问题吗?或者您只是将Selenium用于整个web层(页面、组件)?根据Tapestry文档,使用PageTester是对页面和组件进行单元测试的适当方法: 但这似乎类似于HtmlUnit风格的web测试,因为交互是通过类似web浏览器的界面进行的,而不是通过页面或组件的界面 编辑 我刚刚尝试了

我通常会尽量减少使用Selenium进行测试,并最大限度地利用普通的后端测试(JUnit、mocking)。对于Tapestry,由于回调函数的“魔力”,我发现很难用后一种方式测试页面和组件


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

根据Tapestry文档,使用PageTester是对页面和组件进行单元测试的适当方法:

但这似乎类似于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注释元素,您将需要找到其他解决方案。

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