Testing Tapestry pages and components with JUnit
Asked Answered
H

2

6

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

Hildie answered 27/8, 2010 at 9:7 Comment(0)
U
3

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.

Unwind answered 27/8, 2010 at 12:27 Comment(0)
D
0

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)

Diarrhea answered 2/9, 2017 at 19:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.