Tapestry provides support for easily unit testing your pages and components. Follow the simple steps below.
In order to unit test a page, you'll need to create an instance of PageTester. It acts as both the browser and the servlet container so that you can use it to drive your page.
The PageTester falls into a middle ground between pure unit testing and full-scale integration testing.
As the PageTester is not a real servlet container, you need to tell it the same information as you would in web.xml:
- Your application package.
- Your filter name. This is used to load your Tapestry IoC module only. If you have none, you can pass an empty string or anything to it.
- The folder acting as your context root. This is used to locate your templates (if they're put there).Here is an example (using TestNG, but you're free to use JUnit or anything else):
To test if a page renders properly (optionally with context), you can tell the PageTester to render it and then assert against the DOM Document returned.
Here is an example. Let's assuming the page being tested is named "MyPage" and it should return a page containing an HTML element whose id is "id1" and whose text content should be "hello":
If the page requires a context, you can pass it this way:
After rendering a page, you may want to "click" on an action link and then assert against the resulting page. You can do it this way:
After rendering a page, you may want to fill out a form, submit it and then inspect the resulting page. You can do it this way:
To submit a form by clicking a submit button, call the clickSubmit() method instead.
To unit test a component, just create a test page containing that component. Then unit test that page.