You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Jens Jahnke <je...@wegtam.com> on 2014/03/17 12:00:48 UTC
Testing a panel in isolation (component id not found).
Hi,
I'm trying to test a panel in isolation using `startComponentInPage(C)`.
The test goes as follows:
MyDataModel d = ...
Panel p = new MyCustomPanel("id", new LoadableDataModel(d));
tester.startComponentInPage(p);
Upon execution I get this output:
Unable to find component with id 'id' in [MyCustomPanel [Component id = id]]
Expected: 'id:name'.
Found with similar names: ''
MarkupStream: [markup = file:...]
<wicket:panel xmlns:wicket="http://wicket.apache.org">
<div class="information">
<div class="name">
<strong><span wicket:id="name"></span></strong>
</div>
<div class="meta">
<img class="icon" wicket:id="icon"/>
<span class="genre" wicket:id="genre"></span>
</div>
<div class="description">
<div wicket:id="description"></div>
</div>
</div>
</wicket:panel>, index = 2, current = '<span wicket:id="name">' (line 0, column 0)]
...
Is there a way to get the id right or do I have to provide a custom
page markup for testing?
Regards,
Jens
--
17. Lenzing 2014, 11:58
Homepage : http://www.wegtam.com
This is a country where people are free to practice their religion,
regardless of race, creed, color, obesity, or number of dangling keys...
Re: Testing a panel in isolation (component id not found).
Posted by Jens Jahnke <je...@wegtam.com>.
On Mon, 17 Mar 2014 13:58:59 +0200
Martin Grigorov <mg...@apache.org> wrote:
MG> This method doesn't add the children components.
MG> So trying to render this panel (via #startComponentInPag(panelInstance))
MG> will fail because there are HTML elements with wicket:id in the markup
MG> which have no Java counterparts.
*ouch* Stupid me. Thanks for pointing that out.
Regards,
Jens
--
17. Lenzing 2014, 13:03
Homepage : http://www.wegtam.com
Death is a spirit leaving a body, sort
of like a shell leaving the nut behind.
-- Erma Bombeck
Re: Testing a panel in isolation (component id not found).
Posted by Martin Grigorov <mg...@apache.org>.
OK,
I think I understand now.
Martin Grigorov
Wicket Training and Consulting
On Mon, Mar 17, 2014 at 1:31 PM, Jens Jahnke <je...@wegtam.com> wrote:
> Hi,
>
> On Mon, 17 Mar 2014 13:05:57 +0200
> Martin Grigorov <mg...@apache.org> wrote:
>
> MG> This is the right way.
> MG> Can you show us the code that causes this error ?
> MG> #startComponentInPage() should not fail with such error ...
>
> the exception is thrown at the line with tester.startComponentInPage,
> but here is the code of my custom panel:
>
> public class MyCustomPanel extends Panel {
> protected String name = "";
> protected String icon = "";
> protected String genre = "";
> protected String description = "";
>
> public MyCustomPanel(String id, IModel<MyDataModel> model) {
> super(id, model);
> prepareDefaultVariables(model.getObject());
> }
>
> protected void prepareDefaultVariables(MyDataModel data) {
>
This method doesn't add the children components.
So trying to render this panel (via #startComponentInPag(panelInstance))
will fail because there are HTML elements with wicket:id in the markup
which have no Java counterparts.
> name = data.getName();
> icon = data.getIconName();
> genre = data.getGenre();
> description = data.getDescription();
> }
>
> protected void renderDefaultVariables() {
> add(new Label("name", name));
> PackageResourceReference iconRef = new
> PackageResourceReference(getClass(), icon);
> add(new Image("icon", iconRef));
> add(new Label("genre", genre));
> add(new Label("description", description));
> }
> }
>
> BTW: I'm using wicket 6.12 and are currently unable to upgrade to 6.14. :-|
>
> Regards,
>
> Jens
>
> --
> 17. Lenzing 2014, 12:19
> Homepage : http://www.wegtam.com
>
> SAFETY
> I can live without
> Someone I love
> But not without
> Someone I need.
>
Re: Testing a panel in isolation (component id not found).
Posted by Jens Jahnke <je...@wegtam.com>.
On Mon, 17 Mar 2014 13:38:29 +0200
Martin Grigorov <mg...@apache.org> wrote:
MG> I meant the test code that leads to the exception.
Okay, here it is:
private WicketTester tester;
@Before
public void setUp() throws Exception {
tester = new WicketTester(new MyApplication());
}
@Test
public void testRenderPanel() throws Exception {
MyDataModel d = new MyDataModel("name", "icon.png", "genre", "description");
Panel info = new MyCustomPanel("id", new LoadableDataModel(d));
tester.startComponentInPage(info);
}
And thanks for the hints regarding the other code. I'll rework it accordingly.
Regards,
Jens
--
17. Lenzing 2014, 12:46
Homepage : http://www.wegtam.com
We are all in the gutter, but some of us are looking at the stars.
-- Oscar Wilde
Re: Testing a panel in isolation (component id not found).
Posted by Martin Grigorov <mg...@apache.org>.
Hi,
I meant the test code that leads to the exception.
On Mon, Mar 17, 2014 at 1:31 PM, Jens Jahnke <je...@wegtam.com> wrote:
> Hi,
>
> On Mon, 17 Mar 2014 13:05:57 +0200
> Martin Grigorov <mg...@apache.org> wrote:
>
> MG> This is the right way.
> MG> Can you show us the code that causes this error ?
> MG> #startComponentInPage() should not fail with such error ...
>
> the exception is thrown at the line with tester.startComponentInPage,
> but here is the code of my custom panel:
>
> public class MyCustomPanel extends Panel {
> protected String name = "";
> protected String icon = "";
> protected String genre = "";
> protected String description = "";
>
> public MyCustomPanel(String id, IModel<MyDataModel> model) {
> super(id, model);
> prepareDefaultVariables(model.getObject());
>
This is usually not good.
You better pass the model itself to the children components and let them
pull their values when really needed (e.g. at render phase).
> }
>
> protected void prepareDefaultVariables(MyDataModel data) {
> name = data.getName();
> icon = data.getIconName();
> genre = data.getGenre();
> description = data.getDescription();
> }
>
> protected void renderDefaultVariables() {
> add(new Label("name", name));
>
e.g. replace with:
add(new Label("name", new PropertyModel(getDefaultModel(), "name"));
> PackageResourceReference iconRef = new
> PackageResourceReference(getClass(), icon);
> add(new Image("icon", iconRef));
> add(new Label("genre", genre));
> add(new Label("description", description));
> }
> }
>
> BTW: I'm using wicket 6.12 and are currently unable to upgrade to 6.14. :-|
>
> Regards,
>
> Jens
>
> --
> 17. Lenzing 2014, 12:19
> Homepage : http://www.wegtam.com
>
> SAFETY
> I can live without
> Someone I love
> But not without
> Someone I need.
>
Re: Testing a panel in isolation (component id not found).
Posted by Jens Jahnke <je...@wegtam.com>.
Hi,
On Mon, 17 Mar 2014 13:05:57 +0200
Martin Grigorov <mg...@apache.org> wrote:
MG> This is the right way.
MG> Can you show us the code that causes this error ?
MG> #startComponentInPage() should not fail with such error ...
the exception is thrown at the line with tester.startComponentInPage,
but here is the code of my custom panel:
public class MyCustomPanel extends Panel {
protected String name = "";
protected String icon = "";
protected String genre = "";
protected String description = "";
public MyCustomPanel(String id, IModel<MyDataModel> model) {
super(id, model);
prepareDefaultVariables(model.getObject());
}
protected void prepareDefaultVariables(MyDataModel data) {
name = data.getName();
icon = data.getIconName();
genre = data.getGenre();
description = data.getDescription();
}
protected void renderDefaultVariables() {
add(new Label("name", name));
PackageResourceReference iconRef = new PackageResourceReference(getClass(), icon);
add(new Image("icon", iconRef));
add(new Label("genre", genre));
add(new Label("description", description));
}
}
BTW: I'm using wicket 6.12 and are currently unable to upgrade to 6.14. :-|
Regards,
Jens
--
17. Lenzing 2014, 12:19
Homepage : http://www.wegtam.com
SAFETY
I can live without
Someone I love
But not without
Someone I need.
Re: Testing a panel in isolation (component id not found).
Posted by Martin Grigorov <mg...@apache.org>.
Hi,
This is the right way.
Can you show us the code that causes this error ?
#startComponentInPage() should not fail with such error ...
Martin Grigorov
Wicket Training and Consulting
On Mon, Mar 17, 2014 at 1:00 PM, Jens Jahnke <je...@wegtam.com> wrote:
> Hi,
>
> I'm trying to test a panel in isolation using `startComponentInPage(C)`.
> The test goes as follows:
>
> MyDataModel d = ...
> Panel p = new MyCustomPanel("id", new LoadableDataModel(d));
> tester.startComponentInPage(p);
>
> Upon execution I get this output:
>
> Unable to find component with id 'id' in [MyCustomPanel [Component id =
> id]]
> Expected: 'id:name'.
> Found with similar names: ''
> MarkupStream: [markup = file:...]
> <wicket:panel xmlns:wicket="http://wicket.apache.org">
> <div class="information">
> <div class="name">
> <strong><span wicket:id="name"></span></strong>
> </div>
> <div class="meta">
> <img class="icon" wicket:id="icon"/>
> <span class="genre" wicket:id="genre"></span>
> </div>
> <div class="description">
> <div wicket:id="description"></div>
> </div>
> </div>
> </wicket:panel>, index = 2, current = '<span wicket:id="name">' (line 0,
> column 0)]
> ...
>
> Is there a way to get the id right or do I have to provide a custom
> page markup for testing?
>
> Regards,
>
> Jens
>
> --
> 17. Lenzing 2014, 11:58
> Homepage : http://www.wegtam.com
>
> This is a country where people are free to practice their religion,
> regardless of race, creed, color, obesity, or number of dangling keys...
>