Re: Unit testing Tapestry 4.1 components with TestNG and Easymock

2007-06-04 Thread Ray McBride

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 [EMAIL PROTECTED] 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 [EMAIL PROTECTED] 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

Re: Unit testing Tapestry 4.1 components with TestNG and Easymock

2007-06-01 Thread Ray McBride

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 [EMAIL PROTECTED] 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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]








__
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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]