Java Mailing List Archive

http://www.gg3721.com/

Home » user.jmock »

Re: [jmock-user] mocking 1 method on class, retaining functionality of others?

Matthias Wessendorf

2008-02-27


Author LoginPost Reply
hi paul,

where did you download the 1.2.1version of Jmock ?

-M

On Sat, May 26, 2007 at 2:49 PM, Paul Rooney
<prooney@(protected):
>
>
> Hi,
>
> I'd like to mock a method on a class and keep the functionality of the other
> methods. I can do this in easymock, but can't figure it out with jmock.
> I am using jmock 1.2.1 (core & cglib) and cglib 2.1.3 (due to java 1.4.2
> restriction).
> Anyone got a suggestion?
> Thanks, Paul (details follow...)
>
> e.g.: given class:
> public class FooBar {
>   public String foo() {
>      return "foo";
>   }
>
>   public String bar() {
>      return "bar";
>   }
> }
>
> ====================== As Easy Mock ===============
> public void testEasyMockClassMethod() throws Exception{
>   MockControl ctrl = MockClassControl.createControl(FooBar.class,
>      new Method[] { FooBar.class.getDeclaredMethod("foo", null) } );
>   FooBar mock = (FooBar) ctrl.getMock();
>   mock.foo();
>   ctrl.setReturnValue("Easy Mocked Foo");
>   ctrl.replay();
>
>   System.out.println(mock.foo()); // prints "Easy Mocked Foo"
>   System.out.println(mock.bar()); //prints "bar"
> }
>
> ====================== As JMock ================
> public void testJMockClassMethod() throws Exception{
>   Mock mockFooBar = mock(FooBar.class,"mockFooBar");
>   mockFooBar.expects(once()).method("foo").will( returnValue("JMocked
> Foo"));
>
>   FooBar mock = (FooBar)mockFooBar.proxy();
>   System.out.println("mock.foo()); //prints "JMocked Foo"
>   System.out.println("mock.bar()); //error: unexpected invocation Invoked:
> mockFooBar.bar()
> }
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
>



--
Matthias Wessendorf

further stuff:
blog: http://matthiaswessendorf.wordpress.com/
sessions: http://www.slideshare.net/mwessendorf
mail: matzew-at-apache-dot-org

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

  http://xircles.codehaus.org/manage_email


©2008 gg3721.com - Jax Systems, LLC, U.S.A.