如何使用Tapestry Testify测试Component

时间:2011-03-15 09:14:30

标签: tapestry

我正在用挂毯作证的图书馆进行一些测试。但我对其文档有疑问:http://tapestry.formos.com/nightly/tapestry-testify/testing-components.html

我想将不同的值传递给我的组件的参数。 有人可以向我解释我该怎么做。

我有与文档中相同的项目结构。

myComponent.java

public class myComponent {    
    @Parameter
    @Property
    private String myParam;
}

myComponent.tml

<fieldset xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
    <p>test ${myParam}</p>
</fieldset>

myComponentDemo.java

public class MyComponentDemo {
    @Inject
    @Service("myParam")
    @Property
    private String myParam;    
}   

myComponentDemo.tml

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
    <head>
        <title>DayMonthYearDateInputTestPage</title>
    </head>
    <body>
        <h1 id="h2">DayMonthYearDateInputTestPage</h1>

        <div t:type="myC/MyComponent" t:id="myComponent" t:myParam="myParam"/>
    </body>
</html>

myComponentTest.java

import Perso.monAppli.demo.DemoModule;

import com.formos.tapestry.testify.core.ForComponents;
import com.formos.tapestry.testify.core.TapestryTester;
import com.formos.tapestry.testify.junit3.TapestryTest;

public class MyComponentTest extends TapestryTest {

    @ForComponents(value="myParam")
    private String myParam;

    private static final TapestryTester SHARED_TESTER = new TapestryTester("app", DemoModule.class);

    public MyComponentTest() {
        super(SHARED_TESTER);
    }

    @Test
    public void testElementIsOnPage() {
        Document page = tester.renderPage("demo/MyComponentDemo");
        System.out.println("### HTML " + page.getRootElement().getChildMarkup());
        Assert.assertTrue(page.getRootElement().getChildMarkup().contains("testMyParam"));
    }
}

我是否必须创建一个服务以将值传递给我的组件?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我不确定,但您是否尝试更改“myParam”的名称?

例如:

myComponentDemo.java

@Inject
@Service("myParamService")
@Property
private String myParamValue;

myComponentDemo.tml

<div t:type="myC/MyComponent" t:id="myComponent" t:myParam="myParamValue"/>

myComponentTest.java

@ForComponents("myParamService")
private String myParam;