meSubho | 12 Jan 2012 11:18
Picon

How to mock test method with multi level dependency call


Hi,
I am writing mock junit tests for our existing application. The main class
is a Facade for which I am writing tests currently. In some cases methods of
this class has calls to multi level dependency. For example in method that I
am testing, there is a call like -

      dependency1.get2ndLevelDependency().getXyz();

In this case I am not able to use mock for 2ndLevelDependency and not able
to use expectation on the getXyz() call.

Please suggest what is the best way to handle such scenario.

Thanks,
Subho
--

-- 
View this message in context: http://old.nabble.com/How-to-mock-test-method-with-multi-level-dependency-call-tp33126850p33126850.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

Ignat Zapolsky | 12 Jan 2012 12:35
Picon
Gravatar

Re: How to mock test method with multi level dependency call

Subho,

If you are injecting mock dependency1 object to your facade instance under test then you will be able to capture get2ndLevelDependency call
and return mocked 2nd level dependency object.

With regards, Ignat Zapolsky.

On 12 Jan 2012 10:18, "meSubho" <reachsubho-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:

Hi,
I am writing mock junit tests for our existing application. The main class
is a Facade for which I am writing tests currently. In some cases methods of
this class has calls to multi level dependency. For example in method that I
am testing, there is a call like -

     dependency1.get2ndLevelDependency().getXyz();

In this case I am not able to use mock for 2ndLevelDependency and not able
to use expectation on the getXyz() call.

Please suggest what is the best way to handle such scenario.

Thanks,
Subho
--
View this message in context: http://old.nabble.com/How-to-mock-test-method-with-multi-level-dependency-call-tp33126850p33126850.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


Andy Law | 12 Jan 2012 13:25
Picon
Picon
Favicon

Re: How to mock test method with multi level dependency call


Ignat Zapolsky wrote:
> 
> Subho,
> 
> If you are injecting mock dependency1 object to your facade instance under
> test then you will be able to capture get2ndLevelDependency call
> and return mocked 2nd level dependency object.
> 
> 
> 

But you might be better to refactor dependency1 to have a getXyz() method
(or similar) so that you uncouple your Facade class from the second
dependency.

Later,

Andy
--

-- 
View this message in context: http://old.nabble.com/How-to-mock-test-method-with-multi-level-dependency-call-tp33126850p33127402.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

aro_biz | 12 Jan 2012 13:29
Picon
Favicon

Re: How to mock test method with multi level dependency call

Ignat's right.  You can have a mock return a mock, but it's worth knowing that your second-level dependency is a code smell (a violation of the "Law of Demeter"). 
If you need to have to have a mock return a mock, that's a signal.  Maybe you don't really need dependency1 - you just need to pass in the level 2 dependency directly (or maybe even the 3rd-level dependency, which is the result of getXyz()). Or maybe you need both because you're implementing things in your facade's method that you really should be implementing in a method of dependency1. 

Since you're testing legacy code, you may not have time to re-work the design every time you encounter an issue like this, but it's best to follow Robert C. Martin's "Boy Scout Rule" and leave the code a little better than it was when you found it. 
-Alan

 


Subho,

If you are injecting mock dependency1 object to your facade instance under test then you will be able to capture get2ndLevelDependency call
and return mocked 2nd level dependency object.

With regards, Ignat Zapolsky.

On 12 Jan 2012 10:18, "meSubho" <reachsubho-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:

Hi,
I am writing mock junit tests for our existing application. The main class
is a Facade for which I am writing tests currently. In some cases methods of
this class has calls to multi level dependency. For example in method that I
am testing, there is a call like -

     dependency1.get2ndLevelDependency().getXyz();

In this case I am not able to use mock for 2ndLevelDependency and not able
to use expectation on the getXyz() call.

Please suggest what is the best way to handle such scenario.

Thanks,
Subho
meSubho | 12 Jan 2012 22:19
Picon

Re: How to mock test method with multi level dependency call


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();
	}
	
	
}
==================================

java.lang.AssertionError: unexpected invocation:
dependency.setDependency2(<otherDependency>)
no expectations specified: did you...
 - forget to start an expectation with a cardinality clause?
 - call a mocked method to specify the parameter of an expectation?
what happened before this: nothing!
	at
org.jmock.internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:56)
	at org.jmock.Mockery.dispatch(Mockery.java:218)
	at org.jmock.Mockery.access$000(Mockery.java:43)
	at org.jmock.Mockery$MockObject.invoke(Mockery.java:258)
	at org.jmock.internal.InvocationDiverter.invoke(InvocationDiverter.java:27)
	at org.jmock.internal.FakeObjectMethods.invoke(FakeObjectMethods.java:38)
	at
org.jmock.lib.legacy.ClassImposteriser$4.invoke(ClassImposteriser.java:137)
	at
mypackage.Dependency$$EnhancerByCGLIB$$96d62b77.setDependency2(<generated>)
	at test.FacadeTester.testGetXyz(FacadeTester.java:38)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
	at org.jmock.integration.junit4.JMock$1.invoke(JMock.java:37)
	at
org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:105)
	at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
	at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
	at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
	at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
	at
org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:98)
	at
org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:61)
	at
org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:54)
	at
org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
	at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
	at
org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:52)
	at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

thanks,
Subho

aro_biz wrote:
> 
> Ignat's right. You can have a mock return a mock, but it's worth knowing
> that your second-level dependency is a code smell (a violation of the "Law
> of Demeter"). 
>  If you need to have to have a mock return a mock, that's a signal. Maybe
> you don't really need dependency1 - you just need to pass in the level 2
> dependency directly (or maybe even the 3rd-level dependency, which is the
> result of getXyz()). Or maybe you need both because you're implementing
> things in your facade's method that you really should be implementing in a
> method of dependency1. 
> 
>  Since you're testing legacy code, you may not have time to re-work the
> design every time you encounter an issue like this, but it's best to
> follow Robert C. Martin's "Boy Scout Rule" and leave the code a little
> better than it was when you found it. 
>  -Alan
> 
>  Subho,
>  If you are injecting mock dependency1 object to your facade instance
> under test then you will be able to capture get2ndLevelDependency call
>  and return mocked 2nd level dependency object.
>  With regards, Ignat Zapolsky.
> 
>  On 12 Jan 2012 10:18, "meSubho" < reachsubho@... > wrote:
> 
>  Hi,
>  I am writing mock junit tests for our existing application. The main
> class
>  is a Facade for which I am writing tests currently. In some cases methods
> of
>  this class has calls to multi level dependency. For example in method
> that I
>  am testing, there is a call like -
> 
>  dependency1.get2ndLevelDependency().getXyz();
> 
>  In this case I am not able to use mock for 2ndLevelDependency and not
> able
>  to use expectation on the getXyz() call.
> 
>  Please suggest what is the best way to handle such scenario.
> 
>  Thanks,
>  Subho
> 
> 
http://old.nabble.com/file/p33130708/FacadeTester.java FacadeTester.java 
--

-- 
View this message in context: http://old.nabble.com/How-to-mock-test-method-with-multi-level-dependency-call-tp33126850p33130708.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

Steve Freeman | 12 Jan 2012 22:48
Picon

Re: How to mock test method with multi level dependency call

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

meSubho | 13 Jan 2012 01:20
Picon

Re: How to mock test method with multi level dependency call


Steve,
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))));
			oneOf(dependency1).setDependency2(with(same(dependency2)));
			oneOf(dependency2).getXyz(); will(returnValue("xyz"));
  		}});
		String returnValue = facade.getXyz();
	}

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-tp33126850p33131503.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

Andy Stevens | 13 Jan 2012 08:38
Picon

Re: How to mock test method with multi level dependency call

----- 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

meSubho | 13 Jan 2012 11:57
Picon

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

Julian Bassett | 23 Jan 2012 20:55
Picon

Due datefor 2.6.0

Is there a due date for 2.6.0?

Thanks in advance.

-Julian


Gmane