Re: How to mock test method with multi level dependency call
Thanks again. I have modified the test code according to your suggestion and
its working fine.
<at> Test
public void testGetXyz() {
final Dependency dependency1 = context.mock(Dependency.class);
final OtherDependency dependency2 = context.mock(OtherDependency.class);
facade.setDependency1(dependency1);
context.checking(new Expectations() {{
oneOf(dependency1).getDependency2(); will(returnValue(dependency2));
oneOf(dependency2).getXyz(); will(returnValue("xyz"));
}});
String returnValue = facade.getXyz();
}
I have some doubt about assigning a dependent object as local variable and
invoke methods. I will create a separate post for that.
thanks,
Subho
Bugzilla from stevensa@... wrote:
>
> ----- Original Message -----
> From: "meSubho" <reachsubho@...>
> Sent: Friday, January 13, 2012 12:20 AM
>> thanks for your advice. I have changed the test method as below. Now I am
>> getting NullPointerException.
>>
>> <at> Test
>> public void testGetXyz() {
>> final Dependency dependency1 = context.mock(Dependency.class);
>> final OtherDependency dependency2 = context.mock(OtherDependency.class);
>>
>> facade.setDependency1(dependency1);
>>
>> context.checking(new Expectations() {{
>> oneOf(dependency1).getDependency2();
>> will(returnValue(with(same(dependency2))));
>
> You don't "returnValue with" something, you can check the method was
> called
> with a particular value/class, and you can return a value, but those are
> independent parts of the expectation. Just returnValue(dependency2).
> Or you can expect e.g.
>> oneOf(dependency1).getDependency2(with("Special Value");
>> will(returnValue(dependency2));
>> oneOf(dependency1).getDependency2(with(any(String.class)));
>> will(returnValue(null));
> to only return the dependency in a particular case, and null otherwise.
>
>> oneOf(dependency1).setDependency2(with(same(dependency2)));
>
> As Steve said, dependency1 isn't a real instance of the class, it doesn't
> have real getters & setters. The first expectation is enough to have the
> get method return the second mock, you don't need to set it as well.
> You'll
> only need this expectation if facade's code calls
> dependency1.setDependency2() after calling getDependency2 and you are
> checking that it always gets called with the same object you just returned
> (which, frankly, would be redundant since in a real object it must already
> have that value stored...)
>
>> oneOf(dependency2).getXyz(); will(returnValue("xyz"));
>> }});
>> String returnValue = facade.getXyz();
>> }
>
> Hope that's a bit clearer.
>
>
> Andy
>
>
>>
>> What I understand, in this example, the object relationship are
>> associative
>> and in order to test getXyz() on Facade I need to mock both dependency1
>> and
>> dependency2. Also I am not sure about what you said about the return
>> values.
>> "return a concrete value and . The with(matcher()) clauses are for
>> confirming how a method is called.". When I should use the
>> with(matcher()).
>> Can you please elaborate and give me an example.
>>
>> ==================
>> java.lang.NullPointerException
>> at mypackage.Facade.getXyz(Facade.java:15)
>> at test.FacadeTester.testGetXyz(FacadeTester.java:45)
>> ........
>>
>> Thanks,
>> Subho
>>
>>
>> Steve Freeman-2 wrote:
>>>
>>> The problem is that you're treating dependency1 as a real object when
>>> you
>>> call
>>>
>>> dependency1.setDependency2(dependency2);
>>>
>>> at that point the mockery has had no expectations/allowances set so it
>>> doesn't know what to do with this call.
>>>
>>> Also, you cannot do
>>>
>>> will(returnValue(with(any(String.class))));
>>>
>>> you have to return a concrete value. The with(matcher()) clauses are for
>>> confirming how a method is called.
>>>
>>> You need to be clearer about what is mocked and what is real. Think
>>> about
>>> the relationships between the objects.
>>>
>>> S.
>>>
>>>
>>> On 12 Jan 2012, at 21:19, meSubho wrote:
>>>> Thanks all for such quick response. I am definitely missing something
>>>> here. I
>>>> tried to do a small POC. Please find that attached here.
>>>>
>>>> Here is the test code and I am getting below error when running the
>>>> test.
>>>>
>>>> <at> RunWith(JMock.class)
>>>> public class FacadeTester {
>>>>
>>>> //Mock context
>>>> private Mockery context;
>>>> private Facade facade;
>>>>
>>>> <at> Before
>>>> public void doBeforeEachTestCase() throws Exception {
>>>> context = new JUnit4Mockery() {{
>>>> setImposteriser(ClassImposteriser.INSTANCE);
>>>> }};
>>>>
>>>> facade = new Facade();
>>>> }
>>>>
>>>> <at> Test
>>>> public void testGetXyz() {
>>>> final Dependency dependency1 = context.mock(Dependency.class);
>>>> final OtherDependency dependency2 =
>>>> context.mock(OtherDependency.class);
>>>>
>>>> dependency1.setDependency2(dependency2);
>>>> facade.setDependency1(dependency1);
>>>>
>>>> context.checking(new Expectations() {{
>>>> oneOf(dependency1).getDependency2();
>>>> will(returnValue(with(same(dependency1))));
>>>> oneOf(dependency1).setDependency2(with(same(dependency2)));
>>>> oneOf(dependency2).getXyz();
>>>> will(returnValue(with(any(String.class))));
>>>> }});
>>>> String returnValue = facade.getXyz();
>>>> }
>>>>
>>>>
>>>> }
>>> Steve Freeman
>>>
>>> Winner of the Agile Alliance Gordon Pask award 2006
>>> Book: http://www.growing-object-oriented-software.com
>>>
>>> +44 797 179 4105
>>> Twitter: <at> sf105
>>> Higher Order Logic Limited
>>> Registered office. 2 Church Street, Burnham, Bucks, SL1 7HZ.
>>> Company registered in England & Wales. Number 7522677
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
> http://xircles.codehaus.org/manage_email
>
>
>
>
--
--
View this message in context: http://old.nabble.com/How-to-mock-test-method-with-multi-level-dependency-call-tp33126850p33133490.html
Sent from the jMock - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email