You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Ray McBride <ra...@titanemail.com> on 2007/06/01 15:54:42 UTC

Unit testing Tapestry 4.1 components with TestNG and Easymock

Hi All,

We have recently upgrading one of our apps to Tapestry 4.1 and I have 
been asked to write some unit tests for the existing components.

I am new to Tapestry, TestNG and Easymock, so please bear with me.

I am trying to get the test to run through an if statement, but I keep 
getting the following runtime exception:

java.lang.IllegalStateException: missing behavior definition for the 
preceeding method call getSession(true)

The class for the component is as follows:

public abstract class ViewProductDetail extends BaseComponent
{
@InjectObject("infrastructure:request")
  public abstract WebRequest getRequest();
  public abstract void setRequest(WebRequest webRequest);
 
  @InjectState("visit")
  public abstract Visit getSession();

    public Double getSaving()
  {
    ...

      if(getRequest().getSession(false) != null)
      {
        Visit visit = getSession();
        String discountcode = visit.getDiscountCode();
        Double saving = new Double(rrp.getValue().doubleValue() -     
                    price.getValue(discountcode).doubleValue());
        return saving;
      }
      else
      {
        ...
    }
  }

My Test class is as follows:

public class ViewProductDetailTest extends TestBase
{
  ...
 
  @BeforeClass
  public void setUp() {
    viewProductDetail = newInstance(ViewProductDetail.class);
    webRequest = createMock(WebRequest.class);
    webRequest.getSession(true);
    viewProductDetail.setRequest(webRequest);
  }
 
  @Test (dataProvider = "CreateProduct")
  public void testGetSaving(Product product) {
    try
    {
      viewProductDetail.setProduct(product);
      Double saving = viewProductDetail.getSaving();
      assertEquals(saving, 0.02);
    }
    catch(RuntimeException e){
      System.out.println(e.toString());
    }
}

I'm not sure to the best way to create a session so that my if statement 
will be true. I'm trying to use webRequest.getSession(true), but  it 
doesnt seem to work and I don't know if there is a better way or if I 
have missed something.

Thanks for any help

Ray


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________

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


Re: Unit testing Tapestry 4.1 components with TestNG and Easymock

Posted by Ray McBride <ra...@titanemail.com>.
Thanks for you advice. I think I'm a bit further forward.

viewProductDetail is actually a Tapestry component and is instantiated 
using the com.javaforge.tapestry.testng.TestBase; library.

I have removed the expect call from my testGetSaving method and moved 
the replay/verify to my setUp method.

My main problem now relates to the following calls:

webRequest.getSession(true)
viewProductDetail.setRequest(webRequest)

The Mock WebRequest object gets passed ok with the second call, however 
the as far as the viewProductDetail object is concerned the Session 
doesn't exist and I'm not sure how to get access to it.

Any help will be much appreciated

Ray



Jesse Kuhnert wrote:
> Hmm .....I can't pinpoint the exact problem but if it were up to me I 
> would
> start out by changing things to look more like:
>
> public void setUp() {
>    ...
>    webRequest = createMock(WebRequest.class);
>    expect(webRequest.getSession(true)).andReturn(webSession);
>    ...
>  }
>
> public void testGetSaving(Product product) throws Exception{
>
>      viewProductDetail.setProduct(product); // this looks suspicious - if
> it's a mock then what you are
> // telling easymock is that it should expect someone to call
> setProduct(product) when you invoke getSaving() ?
>      expect(viewProductDetail.getSaving()).andReturn(saving);
>
>      replay();
>
>      saving = viewProductDetail.getSaving();
>
>      verify();
>
>      assertEquals(saving, 0.02);
> }
>
> On 6/1/07, Ray McBride <ra...@titanemail.com> wrote:
>>
>> Hi,
>>
>> Thanks for you quick reply.
>>
>> Sorry, my fault, I must have deleted these while removing my sysouts for
>> this post.
>>
>> I have tried using these but unfortunately receive the same exception. I
>> have also done several searches on google, which seems to suggest the
>> exception is caused by the omission of the replay() method. However
>> their inclusion doesn't seem to change anything.
>>
>> My updated methods are:
>>
>> public void setUp() {
>>     ...
>>     webRequest = createMock(WebRequest.class);
>>     expect(webRequest.getSession(true)).andReturn(webSession);
>>     replay();
>>     webSession = webRequest.getSession(true);
>>     verify();
>>     ...
>>   }
>>
>> public void testGetSaving(Product product) {
>>     try
>>     {
>>       viewProductDetail.setProduct(product);
>>       expect(viewProductDetail.getSaving()).andReturn(saving);
>>       replay();
>>       saving = viewProductDetail.getSaving();
>>       verify();
>>       assertEquals(saving, 0.02);
>>     }
>>     catch(RuntimeException e){
>>       System.out.println(e.toString());
>>     }
>>
>> Thanks for any help
>>
>> Ray
>>
>> Jesse Kuhnert wrote:
>> > You still need to call the replay() / verify() easymock methods, which
>> > can
>> > be examined further here:
>> >
>> > http://easymock.org/EasyMock2_2_Documentation.html
>> >
>> > I think getSession(boolean) also returns a value - so you'd have to
>> > define
>> > what that return value is with a statement like:
>> >
>> > expect(webRequest.getSession(true)).andReturn(session); (or
>> > andReturn(null)
>> > ) .
>> >
>> > On 6/1/07, Ray McBride <ra...@titanemail.com> wrote:
>> >>
>> >> Hi All,
>> >>
>> >> We have recently upgrading one of our apps to Tapestry 4.1 and I have
>> >> been asked to write some unit tests for the existing components.
>> >>
>> >> I am new to Tapestry, TestNG and Easymock, so please bear with me.
>> >>
>> >> I am trying to get the test to run through an if statement, but I 
>> keep
>> >> getting the following runtime exception:
>> >>
>> >> java.lang.IllegalStateException: missing behavior definition for the
>> >> preceeding method call getSession(true)
>> >>
>> >> The class for the component is as follows:
>> >>
>> >> public abstract class ViewProductDetail extends BaseComponent
>> >> {
>> >> @InjectObject("infrastructure:request")
>> >>   public abstract WebRequest getRequest();
>> >>   public abstract void setRequest(WebRequest webRequest);
>> >>
>> >>   @InjectState("visit")
>> >>   public abstract Visit getSession();
>> >>
>> >>     public Double getSaving()
>> >>   {
>> >>     ...
>> >>
>> >>       if(getRequest().getSession(false) != null)
>> >>       {
>> >>         Visit visit = getSession();
>> >>         String discountcode = visit.getDiscountCode();
>> >>         Double saving = new Double(rrp.getValue().doubleValue() -
>> >>                     price.getValue(discountcode).doubleValue());
>> >>         return saving;
>> >>       }
>> >>       else
>> >>       {
>> >>         ...
>> >>     }
>> >>   }
>> >>
>> >> My Test class is as follows:
>> >>
>> >> public class ViewProductDetailTest extends TestBase
>> >> {
>> >>   ...
>> >>
>> >>   @BeforeClass
>> >>   public void setUp() {
>> >>     viewProductDetail = newInstance(ViewProductDetail.class);
>> >>     webRequest = createMock(WebRequest.class);
>> >>     webRequest.getSession(true);
>> >>     viewProductDetail.setRequest(webRequest);
>> >>   }
>> >>
>> >>   @Test (dataProvider = "CreateProduct")
>> >>   public void testGetSaving(Product product) {
>> >>     try
>> >>     {
>> >>       viewProductDetail.setProduct(product);
>> >>       Double saving = viewProductDetail.getSaving();
>> >>       assertEquals(saving, 0.02);
>> >>     }
>> >>     catch(RuntimeException e){
>> >>       System.out.println(e.toString());
>> >>     }
>> >> }
>> >>
>> >> I'm not sure to the best way to create a session so that my if
>> statement
>> >> will be true. I'm trying to use webRequest.getSession(true), but  it
>> >> doesnt seem to work and I don't know if there is a better way or if I
>> >> have missed something.
>> >>
>> >> Thanks for any help
>> >>
>> >> Ray
>> >>
>> >>
>> >> 
>> ______________________________________________________________________
>> >> This email has been scanned by the MessageLabs Email Security System.
>> >> For more information please visit http://www.messagelabs.com/email
>> >> 
>> ______________________________________________________________________
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> >> For additional commands, e-mail: users-help@tapestry.apache.org
>> >>
>> >>
>> >
>> >
>>
>>
>> ______________________________________________________________________
>> This email has been scanned by the MessageLabs Email Security System.
>> For more information please visit http://www.messagelabs.com/email
>> ______________________________________________________________________
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>
>


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________

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


Re: Unit testing Tapestry 4.1 components with TestNG and Easymock

Posted by Jesse Kuhnert <jk...@gmail.com>.
Hmm .....I can't pinpoint the exact problem but if it were up to me I would
start out by changing things to look more like:

public void setUp() {
    ...
    webRequest = createMock(WebRequest.class);
    expect(webRequest.getSession(true)).andReturn(webSession);
    ...
  }

public void testGetSaving(Product product) throws Exception{

      viewProductDetail.setProduct(product); // this looks suspicious - if
it's a mock then what you are
// telling easymock is that it should expect someone to call
setProduct(product) when you invoke getSaving() ?
      expect(viewProductDetail.getSaving()).andReturn(saving);

      replay();

      saving = viewProductDetail.getSaving();

      verify();

      assertEquals(saving, 0.02);
}

On 6/1/07, Ray McBride <ra...@titanemail.com> wrote:
>
> Hi,
>
> Thanks for you quick reply.
>
> Sorry, my fault, I must have deleted these while removing my sysouts for
> this post.
>
> I have tried using these but unfortunately receive the same exception. I
> have also done several searches on google, which seems to suggest the
> exception is caused by the omission of the replay() method. However
> their inclusion doesn't seem to change anything.
>
> My updated methods are:
>
> public void setUp() {
>     ...
>     webRequest = createMock(WebRequest.class);
>     expect(webRequest.getSession(true)).andReturn(webSession);
>     replay();
>     webSession = webRequest.getSession(true);
>     verify();
>     ...
>   }
>
> public void testGetSaving(Product product) {
>     try
>     {
>       viewProductDetail.setProduct(product);
>       expect(viewProductDetail.getSaving()).andReturn(saving);
>       replay();
>       saving = viewProductDetail.getSaving();
>       verify();
>       assertEquals(saving, 0.02);
>     }
>     catch(RuntimeException e){
>       System.out.println(e.toString());
>     }
>
> Thanks for any help
>
> Ray
>
> Jesse Kuhnert wrote:
> > You still need to call the replay() / verify() easymock methods, which
> > can
> > be examined further here:
> >
> > http://easymock.org/EasyMock2_2_Documentation.html
> >
> > I think getSession(boolean) also returns a value - so you'd have to
> > define
> > what that return value is with a statement like:
> >
> > expect(webRequest.getSession(true)).andReturn(session); (or
> > andReturn(null)
> > ) .
> >
> > On 6/1/07, Ray McBride <ra...@titanemail.com> wrote:
> >>
> >> Hi All,
> >>
> >> We have recently upgrading one of our apps to Tapestry 4.1 and I have
> >> been asked to write some unit tests for the existing components.
> >>
> >> I am new to Tapestry, TestNG and Easymock, so please bear with me.
> >>
> >> I am trying to get the test to run through an if statement, but I keep
> >> getting the following runtime exception:
> >>
> >> java.lang.IllegalStateException: missing behavior definition for the
> >> preceeding method call getSession(true)
> >>
> >> The class for the component is as follows:
> >>
> >> public abstract class ViewProductDetail extends BaseComponent
> >> {
> >> @InjectObject("infrastructure:request")
> >>   public abstract WebRequest getRequest();
> >>   public abstract void setRequest(WebRequest webRequest);
> >>
> >>   @InjectState("visit")
> >>   public abstract Visit getSession();
> >>
> >>     public Double getSaving()
> >>   {
> >>     ...
> >>
> >>       if(getRequest().getSession(false) != null)
> >>       {
> >>         Visit visit = getSession();
> >>         String discountcode = visit.getDiscountCode();
> >>         Double saving = new Double(rrp.getValue().doubleValue() -
> >>                     price.getValue(discountcode).doubleValue());
> >>         return saving;
> >>       }
> >>       else
> >>       {
> >>         ...
> >>     }
> >>   }
> >>
> >> My Test class is as follows:
> >>
> >> public class ViewProductDetailTest extends TestBase
> >> {
> >>   ...
> >>
> >>   @BeforeClass
> >>   public void setUp() {
> >>     viewProductDetail = newInstance(ViewProductDetail.class);
> >>     webRequest = createMock(WebRequest.class);
> >>     webRequest.getSession(true);
> >>     viewProductDetail.setRequest(webRequest);
> >>   }
> >>
> >>   @Test (dataProvider = "CreateProduct")
> >>   public void testGetSaving(Product product) {
> >>     try
> >>     {
> >>       viewProductDetail.setProduct(product);
> >>       Double saving = viewProductDetail.getSaving();
> >>       assertEquals(saving, 0.02);
> >>     }
> >>     catch(RuntimeException e){
> >>       System.out.println(e.toString());
> >>     }
> >> }
> >>
> >> I'm not sure to the best way to create a session so that my if
> statement
> >> will be true. I'm trying to use webRequest.getSession(true), but  it
> >> doesnt seem to work and I don't know if there is a better way or if I
> >> have missed something.
> >>
> >> Thanks for any help
> >>
> >> Ray
> >>
> >>
> >> ______________________________________________________________________
> >> This email has been scanned by the MessageLabs Email Security System.
> >> For more information please visit http://www.messagelabs.com/email
> >> ______________________________________________________________________
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> >> For additional commands, e-mail: users-help@tapestry.apache.org
> >>
> >>
> >
> >
>
>
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Jesse Kuhnert
Tapestry/Dojo team member/developer

Open source based consulting work centered around
dojo/tapestry/tacos/hivemind. http://blog.opencomponentry.com

Re: Unit testing Tapestry 4.1 components with TestNG and Easymock

Posted by Ray McBride <ra...@titanemail.com>.
Hi,

Thanks for you quick reply.

Sorry, my fault, I must have deleted these while removing my sysouts for 
this post.

I have tried using these but unfortunately receive the same exception. I 
have also done several searches on google, which seems to suggest the 
exception is caused by the omission of the replay() method. However 
their inclusion doesn't seem to change anything.

My updated methods are:

public void setUp() {
    ...
    webRequest = createMock(WebRequest.class);
    expect(webRequest.getSession(true)).andReturn(webSession);
    replay();
    webSession = webRequest.getSession(true);
    verify();
    ...
  }

public void testGetSaving(Product product) {
    try
    {
      viewProductDetail.setProduct(product);
      expect(viewProductDetail.getSaving()).andReturn(saving);
      replay();
      saving = viewProductDetail.getSaving();
      verify();
      assertEquals(saving, 0.02);
    }
    catch(RuntimeException e){
      System.out.println(e.toString());
    }

Thanks for any help

Ray

Jesse Kuhnert wrote:
> You still need to call the replay() / verify() easymock methods, which 
> can
> be examined further here:
>
> http://easymock.org/EasyMock2_2_Documentation.html
>
> I think getSession(boolean) also returns a value - so you'd have to 
> define
> what that return value is with a statement like:
>
> expect(webRequest.getSession(true)).andReturn(session); (or 
> andReturn(null)
> ) .
>
> On 6/1/07, Ray McBride <ra...@titanemail.com> wrote:
>>
>> Hi All,
>>
>> We have recently upgrading one of our apps to Tapestry 4.1 and I have
>> been asked to write some unit tests for the existing components.
>>
>> I am new to Tapestry, TestNG and Easymock, so please bear with me.
>>
>> I am trying to get the test to run through an if statement, but I keep
>> getting the following runtime exception:
>>
>> java.lang.IllegalStateException: missing behavior definition for the
>> preceeding method call getSession(true)
>>
>> The class for the component is as follows:
>>
>> public abstract class ViewProductDetail extends BaseComponent
>> {
>> @InjectObject("infrastructure:request")
>>   public abstract WebRequest getRequest();
>>   public abstract void setRequest(WebRequest webRequest);
>>
>>   @InjectState("visit")
>>   public abstract Visit getSession();
>>
>>     public Double getSaving()
>>   {
>>     ...
>>
>>       if(getRequest().getSession(false) != null)
>>       {
>>         Visit visit = getSession();
>>         String discountcode = visit.getDiscountCode();
>>         Double saving = new Double(rrp.getValue().doubleValue() -
>>                     price.getValue(discountcode).doubleValue());
>>         return saving;
>>       }
>>       else
>>       {
>>         ...
>>     }
>>   }
>>
>> My Test class is as follows:
>>
>> public class ViewProductDetailTest extends TestBase
>> {
>>   ...
>>
>>   @BeforeClass
>>   public void setUp() {
>>     viewProductDetail = newInstance(ViewProductDetail.class);
>>     webRequest = createMock(WebRequest.class);
>>     webRequest.getSession(true);
>>     viewProductDetail.setRequest(webRequest);
>>   }
>>
>>   @Test (dataProvider = "CreateProduct")
>>   public void testGetSaving(Product product) {
>>     try
>>     {
>>       viewProductDetail.setProduct(product);
>>       Double saving = viewProductDetail.getSaving();
>>       assertEquals(saving, 0.02);
>>     }
>>     catch(RuntimeException e){
>>       System.out.println(e.toString());
>>     }
>> }
>>
>> I'm not sure to the best way to create a session so that my if statement
>> will be true. I'm trying to use webRequest.getSession(true), but  it
>> doesnt seem to work and I don't know if there is a better way or if I
>> have missed something.
>>
>> Thanks for any help
>>
>> Ray
>>
>>
>> ______________________________________________________________________
>> This email has been scanned by the MessageLabs Email Security System.
>> For more information please visit http://www.messagelabs.com/email
>> ______________________________________________________________________
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>
>


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________

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


Re: Unit testing Tapestry 4.1 components with TestNG and Easymock

Posted by Jesse Kuhnert <jk...@gmail.com>.
You still need to call the replay() / verify() easymock methods, which can
be examined further here:

http://easymock.org/EasyMock2_2_Documentation.html

I think getSession(boolean) also returns a value - so you'd have to define
what that return value is with a statement like:

expect(webRequest.getSession(true)).andReturn(session); (or andReturn(null)
) .

On 6/1/07, Ray McBride <ra...@titanemail.com> wrote:
>
> Hi All,
>
> We have recently upgrading one of our apps to Tapestry 4.1 and I have
> been asked to write some unit tests for the existing components.
>
> I am new to Tapestry, TestNG and Easymock, so please bear with me.
>
> I am trying to get the test to run through an if statement, but I keep
> getting the following runtime exception:
>
> java.lang.IllegalStateException: missing behavior definition for the
> preceeding method call getSession(true)
>
> The class for the component is as follows:
>
> public abstract class ViewProductDetail extends BaseComponent
> {
> @InjectObject("infrastructure:request")
>   public abstract WebRequest getRequest();
>   public abstract void setRequest(WebRequest webRequest);
>
>   @InjectState("visit")
>   public abstract Visit getSession();
>
>     public Double getSaving()
>   {
>     ...
>
>       if(getRequest().getSession(false) != null)
>       {
>         Visit visit = getSession();
>         String discountcode = visit.getDiscountCode();
>         Double saving = new Double(rrp.getValue().doubleValue() -
>                     price.getValue(discountcode).doubleValue());
>         return saving;
>       }
>       else
>       {
>         ...
>     }
>   }
>
> My Test class is as follows:
>
> public class ViewProductDetailTest extends TestBase
> {
>   ...
>
>   @BeforeClass
>   public void setUp() {
>     viewProductDetail = newInstance(ViewProductDetail.class);
>     webRequest = createMock(WebRequest.class);
>     webRequest.getSession(true);
>     viewProductDetail.setRequest(webRequest);
>   }
>
>   @Test (dataProvider = "CreateProduct")
>   public void testGetSaving(Product product) {
>     try
>     {
>       viewProductDetail.setProduct(product);
>       Double saving = viewProductDetail.getSaving();
>       assertEquals(saving, 0.02);
>     }
>     catch(RuntimeException e){
>       System.out.println(e.toString());
>     }
> }
>
> I'm not sure to the best way to create a session so that my if statement
> will be true. I'm trying to use webRequest.getSession(true), but  it
> doesnt seem to work and I don't know if there is a better way or if I
> have missed something.
>
> Thanks for any help
>
> Ray
>
>
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Jesse Kuhnert
Tapestry/Dojo team member/developer

Open source based consulting work centered around
dojo/tapestry/tacos/hivemind. http://blog.opencomponentry.com