Another aspect of our test template file is that the template file is an HTML file that might involve testing in an HTML file:
* page field Display test
* Test Link action
* Test form Submission Form
1. page field display test
In our 1th chapter, the start template is essentially asserting that "Hello world!" is displayed String. The test program is shown in Listing 2.5.
package com.kingbegin.web.pages;
Import org.apache.tapestry.dom.Document;
Import Org.apache.tapestry.test.PageTester;
Import Org.junit.Assert;
Import Org.junit.Before;
Import Org.junit.Test;
public class Starttemplatetest {Pagetester tester;
Document Doc;
@Before public void SetUp () throws Exception {String apppackage = "Com.kingbegin.web"; String appName = "APP";
IOC modeule name, default Appmodule.java.
Tester = new Pagetester (apppackage, AppName, "WebRoot");
doc = Tester.renderpage ("Start"); @Test public void Testgetmessage () {String message = Doc.getelementbyid ("Label1"). Getchildmark
Up (). Trim ();
Assert.assertequals (Message, "Hello world!"); }
}
The Pagetester object is a test helper class that takes three parameters to instantiate the Pagetester object: Apppackage is the basic package for the application, AppName is the modeule name of the IOC container default Appmodule.java, our case has no modeule, so its default app, "WebRoot" is the location of the template file. The Renderpage method is to simulate the rendering page to get a Document object, the XHTML file can be considered an XML document, the Document object is the entire page, the document's getElementById ("Label1") Method can help us to get the element object ID "Label1", and then call Getchildmarkup () to get the contents of the elements, because the returned content may also have carriage returns and spaces, so also call the trim () method. The last assertion is that the contents of the Label1 are "helloworld! "。
Tapestry-test-5.0.11.jar
If The page requires a context, and can pass it this way: public
class MyTest extends Ass ert
{
@Test public
void Test1 ()
{
String apppackage = "Org.example.app";
String appName = "Localeapp";
Pagetester tester = new Pagetester (apppackage, AppName, "Src/main/webapp");
object[] context = new object[]{"abc", 123};
Document doc = Tester.invoke (new componentinvocation (New Pagelinktarget ("MyPage"), context);
Assertequals (Doc.getelementbyid ("Id1"). Getchildtext (), "Hello");
}
Http://tapestry.apache.org/tapestry5/tapestry-core/guide/unit-testing-pages.html