Java Mailing List Archive

http://www.gg3721.com/

Home » user.jmock »

[jmock-user] Mocking the same interface with different method return values for subsequent tests.

Igor

2012-04-24

Replies: Find Java Web Hosting

Author LoginPost Reply
Looking for help.
I'm new to jmock and I'm looking for help.
I need to call a method on a mock object which would return different values in
different tests.
Here is an excerpt from my code:
public class MDTest{

  protected Mockery context = new JUnit4Mockery();

...

  @Test
  public void test1()
  {
    final MDStamp mdStamp = context.mock(MDStamp.class);
    setMdStampContext(111,222,333,mdStamp);
    assertEquals(111,mdStamp.getBid());
    assertEquals(222,mdStamp.getAsk());
    assertEquals(333,mdStamp.getLast());
  }

  @Test
  public void test2()
  {
    final MDStamp mdStamp = context.mock(MDStamp.class);
    setMdStampContext(444,555,666,mdStamp);
    assertEquals(444,mdStamp.getBid());
    assertEquals(555,mdStamp.getAsk());
    assertEquals(666,mdStamp.getLast());
  }

  private void setMdStampContext(final int bid, final int ask, final int last,
final MDStamp mdStamp){
    try
    {
       context.checking(new Expectations()
       {
          {
            allowing(mdStamp).getBid();
            with(returnValue(bid));
           
            allowing(mdStamp).getAsk();
            with(returnValue(ask));

            allowing(mdStamp).getLast();
            with(returnValue(last));

          }
        });
    }
    catch (Exception t)
    {
       System.out.println(t);
    }
  }

....

}

I tried it by both defining a separate function(shown above), calling it from
the test method body and embedding the code in the body of the test method.

In both cases assertion fails with actual return values 0 for all 3 methods.

I probably miss something very obvious here as the documentation says that I can
do that. What am I doing wrong here?

Thanks in advance for help.

Igor.


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

  http://xircles.codehaus.org/manage_email


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