easyforgood / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

Spying for chained methods #438

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Mock the real object using spy
2. Call chained method using spy
3. Real method are called

Test Code:

@Test 
public void testSpyChainedCalls()
{
 Foo foo = new Foo();
 Foo fooSpy = spy( foo );

 when( fooSpy.foo().bar() ).thenReturn( 0 );

 int bar = fooSpy.foo().bar();

 assertThat( bar, is( 0 ) );
}

Source Code:

 class Foo
    {

        public Bar foo ()
        {
            return new Bar();
        }
    }

    class Bar
    {
        public int bar() 
        {
            return 3;
        }
    }

What is the expected output? What do you see instead?
Expected the unit test to be passes

What version of the product are you using? On what operating system?
Latest version 1.9.5

Please provide any additional information below.
See the example explained above

Original issue reported on code.google.com by bhaskar....@gmail.com on 11 Jun 2013 at 10:46

GoogleCodeExporter commented 8 years ago
Hi,

You could use a RETURNS_DEEP_STUB answer, but that won't be a spy. I don't 
think that's possible now.

I'd like to add that having a mock returning a mock is wrong, it's breaking the 
Demeter law which means the code forgot to give object behavior responsibility, 
which means fragmented behavior throughout the code.

Anyway if you feel it's that legitimate you can still craft your own answer, 
Mockito is quite extensible :)

Original comment by brice.du...@gmail.com on 13 Nov 2013 at 9:49