Jmock2 Object creation

View: New views
9 Messages — Rating Filter:   Alert me  

Jmock2 Object creation

by ravisankarvivek :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi,

I am new to JMock2 and I am trying to create a Mock object. My code goes like
this. I have a class X which implements a method fun(); The class is perfectly
implemented, no issues on that.

My test case framework:
[code]
public class XTest {

@Test
public void testFun(){
   Mockery context = new JUnit4Mockery();
   X t = context.mock(X.class)

}

}
[/code]

This gives an error msg which says X is not an interface!!


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Jmock2 Object creation

by Nat Pryce :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

It sounds like X is not an interface.

You can either

1) Extract an interface from the class and mock that.

2) Use the jmock-legacy extension that adds support for testing legacy
code by letting you mock classes.

--Nat

2008/7/1 Vivek <vivmbbs@...>:

>
> Hi,
>
> I am new to JMock2 and I am trying to create a Mock object. My code goes like
> this. I have a class X which implements a method fun(); The class is perfectly
> implemented, no issues on that.
>
> My test case framework:
> [code]
> public class XTest {
>
> @Test
> public void testFun(){
>   Mockery context = new JUnit4Mockery();
>   X t = context.mock(X.class)
>
> }
>
> }
> [/code]
>
> This gives an error msg which says X is not an interface!!
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Jmock2 Object creation

by ravisankarvivek :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

So is it like, I can mock only an interface ? Can't I mock a class ?

-Vivek

On Tue, Jul 1, 2008 at 2:59 PM, Nat Pryce <nat.pryce@...> wrote:
It sounds like X is not an interface.

You can either

1) Extract an interface from the class and mock that.

2) Use the jmock-legacy extension that adds support for testing legacy
code by letting you mock classes.

--Nat

2008/7/1 Vivek <vivmbbs@...>:
>
> Hi,
>
> I am new to JMock2 and I am trying to create a Mock object. My code goes like
> this. I have a class X which implements a method fun(); The class is perfectly
> implemented, no issues on that.
>
> My test case framework:
> [code]
> public class XTest {
>
> @Test
> public void testFun(){
>   Mockery context = new JUnit4Mockery();
>   X t = context.mock(X.class)
>
> }
>
> }
> [/code]
>
> This gives an error msg which says X is not an interface!!
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email




Re: Jmock2 Object creation

by Nat Pryce :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Read the second option below.

--Nat

2008/7/1 Vivek R <vivmbbs@...>:

> So is it like, I can mock only an interface ? Can't I mock a class ?
>
> -Vivek
>
> On Tue, Jul 1, 2008 at 2:59 PM, Nat Pryce <nat.pryce@...> wrote:
>>
>> It sounds like X is not an interface.
>>
>> You can either
>>
>> 1) Extract an interface from the class and mock that.
>>
>> 2) Use the jmock-legacy extension that adds support for testing legacy
>> code by letting you mock classes.
>>
>> --Nat
>>
>> 2008/7/1 Vivek <vivmbbs@...>:
>> >
>> > Hi,
>> >
>> > I am new to JMock2 and I am trying to create a Mock object. My code goes
>> > like
>> > this. I have a class X which implements a method fun(); The class is
>> > perfectly
>> > implemented, no issues on that.
>> >
>> > My test case framework:
>> > [code]
>> > public class XTest {
>> >
>> > @Test
>> > public void testFun(){
>> >   Mockery context = new JUnit4Mockery();
>> >   X t = context.mock(X.class)
>> >
>> > }
>> >
>> > }
>> > [/code]
>> >
>> > This gives an error msg which says X is not an interface!!
>> >
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe from this list, please visit:
>> >
>> >    http://xircles.codehaus.org/manage_email
>> >
>> >
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Jmock2 Object creation

by ravisankarvivek :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have mocked the class. Thanks Nat. :).
Have one more query.

I have a function which returns a particular value if u call it say 'x' times. Once you call the function (x + 1)th time, it throws an Exception.
This function f() is present in a class X;

I have created a mock object for class X;

context.checking( new Expectations() {
          exactly(x).of(..........) will (returnValue(new Y()));
});

How do I write the expectation for the (x+1)th time ?

Thanks
Vivek

On Tue, Jul 1, 2008 at 9:22 PM, Nat Pryce <nat.pryce@...> wrote:
Read the second option below.

--Nat

2008/7/1 Vivek R <vivmbbs@...>:
> So is it like, I can mock only an interface ? Can't I mock a class ?
>
> -Vivek
>
> On Tue, Jul 1, 2008 at 2:59 PM, Nat Pryce <nat.pryce@...> wrote:
>>
>> It sounds like X is not an interface.
>>
>> You can either
>>
>> 1) Extract an interface from the class and mock that.
>>
>> 2) Use the jmock-legacy extension that adds support for testing legacy
>> code by letting you mock classes.
>>
>> --Nat
>>
>> 2008/7/1 Vivek <vivmbbs@...>:
>> >
>> > Hi,
>> >
>> > I am new to JMock2 and I am trying to create a Mock object. My code goes
>> > like
>> > this. I have a class X which implements a method fun(); The class is
>> > perfectly
>> > implemented, no issues on that.
>> >
>> > My test case framework:
>> > [code]
>> > public class XTest {
>> >
>> > @Test
>> > public void testFun(){
>> >   Mockery context = new JUnit4Mockery();
>> >   X t = context.mock(X.class)
>> >
>> > }
>> >
>> > }
>> > [/code]
>> >
>> > This gives an error msg which says X is not an interface!!
>> >
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe from this list, please visit:
>> >
>> >    http://xircles.codehaus.org/manage_email
>> >
>> >
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email




Re: Jmock2 Object creation

by Nat Pryce :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2008/7/2 Vivek R <vivmbbs@...>:

> I have a function which returns a particular value if u call it say 'x'
> times. Once you call the function (x + 1)th time, it throws an Exception.
> This function f() is present in a class X;
>
> I have created a mock object for class X;
>
> context.checking( new Expectations() {
>           exactly(x).of(..........) will (returnValue(new Y()));
> });
>
> How do I write the expectation for the (x+1)th time ?

Just add another expectation after the first.  The first expectation
will match until it has been called x times.  After that, the next
will match.  If you write it as an "allowing" or "atLeast(1).of"
statement, then every call afterwards will throw an exception.

For example:

context.checking( new Expectations() {{
          exactly(x).of(m).foo(); will (returnValue(new Thing()));
          atLeast(1).of(m).foo(); will (throwException(new
BlahDeBlahException());
}});

More information is here: http://www.jmock.org/dispatch.html

--Nat

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Jmock2 Object creation

by ravisankarvivek :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Am sorry, I think I dint frame the question properly. Let me make it clear & precise :)

Class X{
  Y service;   
  public void setServ(Y serv){
         service = serv;
   }
  public void f() throws AppException {
        
          for( int i = 0; i < 3;i++ ){
                   // do something
           service.get();
         }

   }
};

In my testFile:

I mock the class Y and create a mock object named mock_service;
I then say X xx; xx.setServ(mock_service);

context.checking( new Expectations() {{
        exactly(3).of(mock_service).get(); will(.... );

}});

xx.f();

It says AssertionError: Expected AppException. How do I resolve this ?
On Wed, Jul 2, 2008 at 8:19 PM, Nat Pryce <nat.pryce@...> wrote:
2008/7/2 Vivek R <vivmbbs@...>:
> I have a function which returns a particular value if u call it say 'x'
> times. Once you call the function (x + 1)th time, it throws an Exception.
> This function f() is present in a class X;
>
> I have created a mock object for class X;
>
> context.checking( new Expectations() {
>           exactly(x).of(..........) will (returnValue(new Y()));
> });
>
> How do I write the expectation for the (x+1)th time ?

Just add another expectation after the first.  The first expectation
will match until it has been called x times.  After that, the next
will match.  If you write it as an "allowing" or "atLeast(1).of"
statement, then every call afterwards will throw an exception.

For example:

context.checking( new Expectations() {{
         exactly(x).of(m).foo(); will (returnValue(new Thing()));
         atLeast(1).of(m).foo(); will (throwException(new
BlahDeBlahException());
}});

More information is here: http://www.jmock.org/dispatch.html

--Nat

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email




Re: Jmock2 Object creation

by Nat Pryce :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

JMock doesn't expect exceptions.  Have you set the expected parameter
of the @Test annotation?

--Nat

2008/7/2 Vivek R <vivmbbs@...>:

> Am sorry, I think I dint frame the question properly. Let me make it clear &
> precise :)
>
> Class X{
>   Y service;
>   public void setServ(Y serv){
>          service = serv;
>    }
>   public void f() throws AppException {
>
>           for( int i = 0; i < 3;i++ ){
>                    // do something
>            service.get();
>          }
>
>    }
> };
>
> In my testFile:
>
> I mock the class Y and create a mock object named mock_service;
> I then say X xx; xx.setServ(mock_service);
>
> context.checking( new Expectations() {{
>         exactly(3).of(mock_service).get(); will(.... );
>
> }});
>
> xx.f();
>
> It says AssertionError: Expected AppException. How do I resolve this ?
> On Wed, Jul 2, 2008 at 8:19 PM, Nat Pryce <nat.pryce@...> wrote:
>>
>> 2008/7/2 Vivek R <vivmbbs@...>:
>> > I have a function which returns a particular value if u call it say 'x'
>> > times. Once you call the function (x + 1)th time, it throws an
>> > Exception.
>> > This function f() is present in a class X;
>> >
>> > I have created a mock object for class X;
>> >
>> > context.checking( new Expectations() {
>> >           exactly(x).of(..........) will (returnValue(new Y()));
>> > });
>> >
>> > How do I write the expectation for the (x+1)th time ?
>>
>> Just add another expectation after the first.  The first expectation
>> will match until it has been called x times.  After that, the next
>> will match.  If you write it as an "allowing" or "atLeast(1).of"
>> statement, then every call afterwards will throw an exception.
>>
>> For example:
>>
>> context.checking( new Expectations() {{
>>          exactly(x).of(m).foo(); will (returnValue(new Thing()));
>>          atLeast(1).of(m).foo(); will (throwException(new
>> BlahDeBlahException());
>> }});
>>
>> More information is here: http://www.jmock.org/dispatch.html
>>
>> --Nat
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Jmock2 Object creation

by ravisankarvivek :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Oh..Yeah... Got it! Thanks a lot :)
- Vivek


On Wed, Jul 2, 2008 at 10:17 PM, Nat Pryce <nat.pryce@...> wrote:
JMock doesn't expect exceptions.  Have you set the expected parameter
of the @Test annotation?

--Nat

2008/7/2 Vivek R <vivmbbs@...>:
> Am sorry, I think I dint frame the question properly. Let me make it clear &
> precise :)
>
> Class X{
>   Y service;
>   public void setServ(Y serv){
>          service = serv;
>    }
>   public void f() throws AppException {
>
>           for( int i = 0; i < 3;i++ ){
>                    // do something
>            service.get();
>          }
>
>    }
> };
>
> In my testFile:
>
> I mock the class Y and create a mock object named mock_service;
> I then say X xx; xx.setServ(mock_service);
>
> context.checking( new Expectations() {{
>         exactly(3).of(mock_service).get(); will(.... );
>
> }});
>
> xx.f();
>
> It says AssertionError: Expected AppException. How do I resolve this ?
> On Wed, Jul 2, 2008 at 8:19 PM, Nat Pryce <nat.pryce@...> wrote:
>>
>> 2008/7/2 Vivek R <vivmbbs@...>:
>> > I have a function which returns a particular value if u call it say 'x'
>> > times. Once you call the function (x + 1)th time, it throws an
>> > Exception.
>> > This function f() is present in a class X;
>> >
>> > I have created a mock object for class X;
>> >
>> > context.checking( new Expectations() {
>> >           exactly(x).of(..........) will (returnValue(new Y()));
>> > });
>> >
>> > How do I write the expectation for the (x+1)th time ?
>>
>> Just add another expectation after the first.  The first expectation
>> will match until it has been called x times.  After that, the next
>> will match.  If you write it as an "allowing" or "atLeast(1).of"
>> statement, then every call afterwards will throw an exception.
>>
>> For example:
>>
>> context.checking( new Expectations() {{
>>          exactly(x).of(m).foo(); will (returnValue(new Thing()));
>>          atLeast(1).of(m).foo(); will (throwException(new
>> BlahDeBlahException());
>> }});
>>
>> More information is here: http://www.jmock.org/dispatch.html
>>
>> --Nat
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



LightInTheBox - Buy quality products at wholesale price