使用Wicket Tester时,如何在单元测试中保持组件路径不变

Mic*_*l-7 5 java datatable wicket wicket-tester

我有几个wicket测试,它们针对可排序的DataTable,特别是ajax-单击可排序的列标题并断言渲染的主体行的内容.现在,表组件的后代的组件层次结构由wicket框架自动生成,并导致排序链接(ajax)的路径类似于:

table:topToolbars:toolbars:0:headers:1:header:orderByLink
Run Code Online (Sandbox Code Playgroud)

但是,当DataTable跨测试重新呈现时,工具栏组件的索引每次都会递增,即类似于:

table:topToolbars:toolbars:1:headers:1:header:orderByLink
Run Code Online (Sandbox Code Playgroud)

然后打破后续测试的硬编码路径,因为它们将不再匹配.

数据表构造的代码片段如下:

        final PayeesProvider dataProvider = new PayeesProvider();
    table = new DataTable<ResponsePayeeDetails>("payees", columns, dataProvider, rowsPerPage);
    table.setOutputMarkupId(true);
    table.addTopToolbar(new AjaxFallbackHeadersToolbar(table, dataProvider) {

        private static final long serialVersionUID = -3509487788284410429L;

        @Override
        protected WebMarkupContainer newSortableHeader(final String borderId, final String property, final ISortStateLocator locator) {
            return new AjaxFallbackOrderByBorder(borderId, property, locator, getAjaxCallDecorator()) {

                @Override
                protected void onRender() {
                    System.out.printf("Path: %s\n", this.getPageRelativePath());
                    super.onRender();
                }

                private static final long serialVersionUID = -6399737639959498915L;

                @Override
                protected void onAjaxClick(final AjaxRequestTarget target) {
                    target.add(getTable(), navigator, navigatorInfoContainer);
                }

                @Override
                protected void onSortChanged() {
                    super.onSortChanged();
                    getTable().setCurrentPage(0);
                }
            };
        }
    });
    table.addBottomToolbar(new NoRecordsToolbar(table));
    add(table);
Run Code Online (Sandbox Code Playgroud)

确切地说,当我运行我的测试时,上面的System.out.printf语句打印:

(第1次测试)

Path: payees:topToolbars:toolbars:0:headers:1:header
Path: payees:topToolbars:toolbars:0:headers:2:header
Run Code Online (Sandbox Code Playgroud)

(第2次测试)

Path: payees:topToolbars:toolbars:2:headers:1:header
Path: payees:topToolbars:toolbars:2:headers:2:header
Run Code Online (Sandbox Code Playgroud)

(第3次测试)

Path: payees:topToolbars:toolbars:4:headers:1:header
Path: payees:topToolbars:toolbars:4:headers:2:header
Run Code Online (Sandbox Code Playgroud)

(第4次测试)

Path: payees:topToolbars:toolbars:6:headers:1:header
Path: payees:topToolbars:toolbars:6:headers:2:header
Path: payees:topToolbars:toolbars:6:headers:1:header
Path: payees:topToolbars:toolbars:6:headers:2:header
Path: payees:topToolbars:toolbars:6:headers:1:header
Path: payees:topToolbars:toolbars:6:headers:2:header
Run Code Online (Sandbox Code Playgroud)

(第5次测试)

Path: payees:topToolbars:toolbars:8:headers:1:header
Path: payees:topToolbars:toolbars:8:headers:2:header
Run Code Online (Sandbox Code Playgroud)

有谁知道如何强制索引生成更具确定性/可重复性.或者,是否有一种野性梳理或以其他方式推广路径的方法,以使它们免受这些增量的影响?

任何帮助将非常感谢chaps!

小智 0

DataTable ItemReuseStrategy由于 default ,每次刷新表时都会生成全新的项目,因此id 每次都会递增。ItemReuseStrategy如果保持 id 相同是首要任务,那么您可能会幸运地实现自定义。

我们遇到了类似的问题,并制定了与您的替代问题相符的策略。有了这个,您可以要求测试检查例如最后渲染页面上的第三行。注意:代码是 Scala 语言。

简而言之,为了解决这个问题,我们实施了一种findNthComponentOnPage增强WicketTester.

/**
  * @param id Wicket id of component to find
  * @param n 1-based
  * @tparam T class of component to find
  * @return the Nth component of class T appearing on the lastRenderedPage
  */
def findNthComponentOnPage[T <: Component : ClassTag](id: String, n: Int): T = {
  tester.getLastRenderedPage.visitChildren(classTag[T].runtimeClass, new IVisitor[T, T] {
    var count = 0

    override def component(checkbox: T, visit: IVisit[T]): Unit = {
      if (checkbox.getId == id) {
        count += 1
        if (count == n) {
          visit.stop(checkbox)
        }

      }
    }
  })
}
Run Code Online (Sandbox Code Playgroud)