Tapestry 5 Unit Test Example

Source: Internet
Author: User
Tags assert trim

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.