Java Mailing List Archive

http://www.gg3721.com/

Home » user.jmock »

[jmock-user] Mock a interface and get values from a implementation.

elias

2012-09-19

Replies: Find Java Web Hosting

Author LoginPost Reply
Hello Guys,

I have this question and I have been trying to get a solution for long
time now but I'm still unanswered.

I want to mock an interface and create expectations just for some methods
but all other methods, to return values from the implementation.

For example, say this interface:

public interface Calculator {
  public double sum(double a, double b, double c);
  public double multiply(double a, double b, double c);
  public double substract(double a, double b, double c);
  public double divide(double a, double b, double c);
}

Then I want to mock only the sum method so I create:

context.checking(new Expectations() {{
  oneOf(calculator).sum(3,4,5);
  will(returnValue((3*3*5.0)));
}});

and the test:

double result = calculator.sum(3,4,5);
assertEquals(3*3*5.0, result);

double resultd = calculator.multiply(3,4,5);
assertEquals(3*4*5.0, resultd);

The second call to calculator will fail because an unexpected invocation
to multiply. I can add an expectation to the multiply method like this
one:

allowing(calculator).multiply(with(any(Double.class)),
            with(any(Double.class)),
                   with(any(Double.class)));

but this always returns 0.0 as result.

What I want is to get the actual real value from multiply as if I were
calling the implementation of Calculator. If I call
calculator.multiply(1,1,1) I should get 1, if calculator.multiply(3,4,5) I
should get 60, and so on for every value.

Is there a way to achieve this?

Thanks so much for your help.




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

  http://xircles.codehaus.org/manage_email


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