You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Zbynek Vavros <zb...@gmail.com> on 2019/02/03 15:20:38 UTC

WicketTester and stateless component

I have simple page for displaying messages:

@MountPath("/wicket/page/error")
public class ErrorPage extends WebPage {

    private String message;

    public ErrorPage() {
        this(null);
    }

    public ErrorPage(String msg) {
        if(StringUtils.hasText(msg)) {
            this.message = msg;
        } else {
            this.message = getString("sapi2.page.error.default_message");
        }
        add(new Label("errorMsg",
this.message).setEscapeModelStrings(false));
    }

    public String getMessage() {
        return message;
    }
}

now I would like to test this with WickeTester:

@Testpublic void testPageRenders() {

        wicketTester.startPage(new ErrorPage(message));
        ErrorPage errorPage = (ErrorPage)
wicketTester.getLastRenderedPage();
        assertEquals(message, errorPage.getMessage());
}

but the test is failing and I can see the constructor is called twice, once
with my "message" parameter (as expected) and then again via empty
constructor and thus the errorPage.getMessage() returns null. Why is the
empty constructor called at all? After some painful debugging I suspect it
has something to do with the page being stateless...

Zbynek

Re: WicketTester and stateless component

Posted by Zbynek Vavros <zb...@gmail.com>.
Thanks Sven, nice to know I was "close" :)

And let me express the gratitude to all Wicket community, getting help like
this is
why I struggle to move to some new fancy framework instead of good old
Wicket.

Zbynek

On Sun, Feb 3, 2019 at 7:24 PM Sven Meier <sv...@meiers.net> wrote:

> Hi,
>
> indeed, since your page is bookmarkable (has a default constructor) and
> is stateless, Wicket redirects to a fresh instance for render.
>
> You can prevent this by disabling statelessness:
>
>          if(StringUtils.hasText(msg)) {
>              this.message = msg;
>              setStatelessHint(false);
>          } else {
>              this.message = getString("sapi2.page.error.default_message");
>          }
>
> Have fun
> Sven
>
>
> Am 03.02.19 um 16:20 schrieb Zbynek Vavros:
> > I have simple page for displaying messages:
> >
> > @MountPath("/wicket/page/error")
> > public class ErrorPage extends WebPage {
> >
> >      private String message;
> >
> >      public ErrorPage() {
> >          this(null);
> >      }
> >
> >      public ErrorPage(String msg) {
> >          if(StringUtils.hasText(msg)) {
> >              this.message = msg;
> >          } else {
> >              this.message =
> getString("sapi2.page.error.default_message");
> >          }
> >          add(new Label("errorMsg",
> > this.message).setEscapeModelStrings(false));
> >      }
> >
> >      public String getMessage() {
> >          return message;
> >      }
> > }
> >
> > now I would like to test this with WickeTester:
> >
> > @Testpublic void testPageRenders() {
> >
> >          wicketTester.startPage(new ErrorPage(message));
> >          ErrorPage errorPage = (ErrorPage)
> > wicketTester.getLastRenderedPage();
> >          assertEquals(message, errorPage.getMessage());
> > }
> >
> > but the test is failing and I can see the constructor is called twice,
> once
> > with my "message" parameter (as expected) and then again via empty
> > constructor and thus the errorPage.getMessage() returns null. Why is the
> > empty constructor called at all? After some painful debugging I suspect
> it
> > has something to do with the page being stateless...
> >
> > Zbynek
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: WicketTester and stateless component

Posted by Sven Meier <sv...@meiers.net>.
Hi,

indeed, since your page is bookmarkable (has a default constructor) and 
is stateless, Wicket redirects to a fresh instance for render.

You can prevent this by disabling statelessness:

         if(StringUtils.hasText(msg)) {
             this.message = msg;
             setStatelessHint(false);
         } else {
             this.message = getString("sapi2.page.error.default_message");
         }

Have fun
Sven


Am 03.02.19 um 16:20 schrieb Zbynek Vavros:
> I have simple page for displaying messages:
>
> @MountPath("/wicket/page/error")
> public class ErrorPage extends WebPage {
>
>      private String message;
>
>      public ErrorPage() {
>          this(null);
>      }
>
>      public ErrorPage(String msg) {
>          if(StringUtils.hasText(msg)) {
>              this.message = msg;
>          } else {
>              this.message = getString("sapi2.page.error.default_message");
>          }
>          add(new Label("errorMsg",
> this.message).setEscapeModelStrings(false));
>      }
>
>      public String getMessage() {
>          return message;
>      }
> }
>
> now I would like to test this with WickeTester:
>
> @Testpublic void testPageRenders() {
>
>          wicketTester.startPage(new ErrorPage(message));
>          ErrorPage errorPage = (ErrorPage)
> wicketTester.getLastRenderedPage();
>          assertEquals(message, errorPage.getMessage());
> }
>
> but the test is failing and I can see the constructor is called twice, once
> with my "message" parameter (as expected) and then again via empty
> constructor and thus the errorPage.getMessage() returns null. Why is the
> empty constructor called at all? After some painful debugging I suspect it
> has something to do with the page being stateless...
>
> Zbynek
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org