You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Per Newgro <pe...@gmx.ch> on 2012/11/01 19:24:35 UTC

What is the way to test a behavior?

Hi,

i try to unit test a custom behavior. But i was wondering what's the 
right way to test it.

I provide some code to explain my mind mismatch. The behavior shall 
interrupt the rendering
of a component. I would like to answer this with a 404. Calling the 
behavior method directly
results in the expected exception. But if i render a using component 
it's not answered by the 404.

How can i get a 404 within the usage testcase (see below).

Thanks for helping me
Per

<code>
public class TestHomePage {

     public static class MyBehavior extends Behavior {
         @Override
         public void beforeRender(Component component) {
             if (true) {
                 throw new 
AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, "This 
is for testing");
             }
         }
     }

     public static class MyTestHomePage extends WebPage implements 
IMarkupResourceStreamProvider {

         public MyTestHomePage() {
             add(new MyBehavior());
         }

         @Override
         public IResourceStream getMarkupResourceStream(
                 MarkupContainer container, Class<?> containerClass) {
             return Markup.of("<html></html>").getMarkupResourceStream();
         }
     }

     private WicketTester tester;

     @Before
     public void setUp() {
         tester = new WicketTester(new WicketApplication());
     }

     @Test
     public void abortRenderingOnUsage() {
         tester.startPage(MyTestHomePage.class);
         tester.assertRenderedPage(InternalErrorPage.class);
     }

     @Test(expected = AbortWithHttpErrorCodeException.class)
     public void abortRenderingOnUnitTesting() {
         MyBehavior behavior = new MyBehavior();
         behavior.beforeRender(null);
     }
}
</code>

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


Re: What is the way to test a behavior?

Posted by vineet semwal <vi...@gmail.com>.
hi martin,
you missed his test "abortRenderingOnUsage()"
 tester.executeBehavior() doesn't fit his case,
also tester#executeBehavior(AbstractAjaxBehavior behavior)


On Fri, Nov 2, 2012 at 1:49 PM, Martin Grigorov <mg...@apache.org> wrote:
> Hi,
>
> Additionally you need to : tester.executeBehavior() otherwise
> WicketTester is not used at all in your test.
>
> On Thu, Nov 1, 2012 at 10:49 PM, vineet semwal <vi...@gmail.com> wrote:
>> hi,
>> this is expected behavior,
>> AbortWithHttpErrorCodeException doesn't respond with
>> InternalErrorPage,it merely sets HTTP error code ,
>> you can assert that by below code
>>
>>  Assert.assertEquals( tester.getLastResponse().getStatus(),
>> HttpServletResponse.SC_NOT_FOUND);
>> Assert.assertEquals(tester.getLastResponse().getErrorMessage(),
>> "This is for testing") ;
>>
>>
>> On Thu, Nov 1, 2012 at 11:54 PM, Per Newgro <pe...@gmx.ch> wrote:
>>> Hi,
>>>
>>> i try to unit test a custom behavior. But i was wondering what's the right
>>> way to test it.
>>>
>>> I provide some code to explain my mind mismatch. The behavior shall
>>> interrupt the rendering
>>> of a component. I would like to answer this with a 404. Calling the behavior
>>> method directly
>>> results in the expected exception. But if i render a using component it's
>>> not answered by the 404.
>>>
>>> How can i get a 404 within the usage testcase (see below).
>>>
>>> Thanks for helping me
>>> Per
>>>
>>> <code>
>>> public class TestHomePage {
>>>
>>>     public static class MyBehavior extends Behavior {
>>>         @Override
>>>         public void beforeRender(Component component) {
>>>             if (true) {
>>>                 throw new
>>> AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, "This is
>>> for testing");
>>>             }
>>>         }
>>>     }
>>>
>>>     public static class MyTestHomePage extends WebPage implements
>>> IMarkupResourceStreamProvider {
>>>
>>>         public MyTestHomePage() {
>>>             add(new MyBehavior());
>>>         }
>>>
>>>         @Override
>>>         public IResourceStream getMarkupResourceStream(
>>>                 MarkupContainer container, Class<?> containerClass) {
>>>             return Markup.of("<html></html>").getMarkupResourceStream();
>>>         }
>>>     }
>>>
>>>     private WicketTester tester;
>>>
>>>     @Before
>>>     public void setUp() {
>>>         tester = new WicketTester(new WicketApplication());
>>>     }
>>>
>>>     @Test
>>>     public void abortRenderingOnUsage() {
>>>         tester.startPage(MyTestHomePage.class);
>>>         tester.assertRenderedPage(InternalErrorPage.class);
>>>     }
>>>
>>>     @Test(expected = AbortWithHttpErrorCodeException.class)
>>>     public void abortRenderingOnUnitTesting() {
>>>         MyBehavior behavior = new MyBehavior();
>>>         behavior.beforeRender(null);
>>>     }
>>> }
>>> </code>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>>> For additional commands, e-mail: users-help@wicket.apache.org
>>>
>>
>>
>>
>> --
>> regards,
>>
>> Vineet Semwal
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>
>
>
> --
> Martin Grigorov
> jWeekend
> Training, Consulting, Development
> http://jWeekend.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>



-- 
regards,

Vineet Semwal

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


Re: What is the way to test a behavior?

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

Additionally you need to : tester.executeBehavior() otherwise
WicketTester is not used at all in your test.

On Thu, Nov 1, 2012 at 10:49 PM, vineet semwal <vi...@gmail.com> wrote:
> hi,
> this is expected behavior,
> AbortWithHttpErrorCodeException doesn't respond with
> InternalErrorPage,it merely sets HTTP error code ,
> you can assert that by below code
>
>  Assert.assertEquals( tester.getLastResponse().getStatus(),
> HttpServletResponse.SC_NOT_FOUND);
> Assert.assertEquals(tester.getLastResponse().getErrorMessage(),
> "This is for testing") ;
>
>
> On Thu, Nov 1, 2012 at 11:54 PM, Per Newgro <pe...@gmx.ch> wrote:
>> Hi,
>>
>> i try to unit test a custom behavior. But i was wondering what's the right
>> way to test it.
>>
>> I provide some code to explain my mind mismatch. The behavior shall
>> interrupt the rendering
>> of a component. I would like to answer this with a 404. Calling the behavior
>> method directly
>> results in the expected exception. But if i render a using component it's
>> not answered by the 404.
>>
>> How can i get a 404 within the usage testcase (see below).
>>
>> Thanks for helping me
>> Per
>>
>> <code>
>> public class TestHomePage {
>>
>>     public static class MyBehavior extends Behavior {
>>         @Override
>>         public void beforeRender(Component component) {
>>             if (true) {
>>                 throw new
>> AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, "This is
>> for testing");
>>             }
>>         }
>>     }
>>
>>     public static class MyTestHomePage extends WebPage implements
>> IMarkupResourceStreamProvider {
>>
>>         public MyTestHomePage() {
>>             add(new MyBehavior());
>>         }
>>
>>         @Override
>>         public IResourceStream getMarkupResourceStream(
>>                 MarkupContainer container, Class<?> containerClass) {
>>             return Markup.of("<html></html>").getMarkupResourceStream();
>>         }
>>     }
>>
>>     private WicketTester tester;
>>
>>     @Before
>>     public void setUp() {
>>         tester = new WicketTester(new WicketApplication());
>>     }
>>
>>     @Test
>>     public void abortRenderingOnUsage() {
>>         tester.startPage(MyTestHomePage.class);
>>         tester.assertRenderedPage(InternalErrorPage.class);
>>     }
>>
>>     @Test(expected = AbortWithHttpErrorCodeException.class)
>>     public void abortRenderingOnUnitTesting() {
>>         MyBehavior behavior = new MyBehavior();
>>         behavior.beforeRender(null);
>>     }
>> }
>> </code>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>
>
>
> --
> regards,
>
> Vineet Semwal
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>



-- 
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com

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


Re: What is the way to test a behavior?

Posted by Per Newgro <pe...@gmx.ch>.
Thanks, works like expected. You right. If i throw another exception 
(ex. IAE) the assertion on rendered page is working. So checking the 
last response status seems to be "natural".

Per

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


Re: What is the way to test a behavior?

Posted by vineet semwal <vi...@gmail.com>.
hi,
this is expected behavior,
AbortWithHttpErrorCodeException doesn't respond with
InternalErrorPage,it merely sets HTTP error code ,
you can assert that by below code

 Assert.assertEquals( tester.getLastResponse().getStatus(),
HttpServletResponse.SC_NOT_FOUND);
Assert.assertEquals(tester.getLastResponse().getErrorMessage(),
"This is for testing") ;


On Thu, Nov 1, 2012 at 11:54 PM, Per Newgro <pe...@gmx.ch> wrote:
> Hi,
>
> i try to unit test a custom behavior. But i was wondering what's the right
> way to test it.
>
> I provide some code to explain my mind mismatch. The behavior shall
> interrupt the rendering
> of a component. I would like to answer this with a 404. Calling the behavior
> method directly
> results in the expected exception. But if i render a using component it's
> not answered by the 404.
>
> How can i get a 404 within the usage testcase (see below).
>
> Thanks for helping me
> Per
>
> <code>
> public class TestHomePage {
>
>     public static class MyBehavior extends Behavior {
>         @Override
>         public void beforeRender(Component component) {
>             if (true) {
>                 throw new
> AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, "This is
> for testing");
>             }
>         }
>     }
>
>     public static class MyTestHomePage extends WebPage implements
> IMarkupResourceStreamProvider {
>
>         public MyTestHomePage() {
>             add(new MyBehavior());
>         }
>
>         @Override
>         public IResourceStream getMarkupResourceStream(
>                 MarkupContainer container, Class<?> containerClass) {
>             return Markup.of("<html></html>").getMarkupResourceStream();
>         }
>     }
>
>     private WicketTester tester;
>
>     @Before
>     public void setUp() {
>         tester = new WicketTester(new WicketApplication());
>     }
>
>     @Test
>     public void abortRenderingOnUsage() {
>         tester.startPage(MyTestHomePage.class);
>         tester.assertRenderedPage(InternalErrorPage.class);
>     }
>
>     @Test(expected = AbortWithHttpErrorCodeException.class)
>     public void abortRenderingOnUnitTesting() {
>         MyBehavior behavior = new MyBehavior();
>         behavior.beforeRender(null);
>     }
> }
> </code>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>



-- 
regards,

Vineet Semwal

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