简体   繁体   中英

Testing Tapestry pages and components with JUnit

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)?

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM