mensahs / mockito

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

unexpected spy() behaviour #333

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

import junit.framework.Assert;

import org.junit.Test;
import org.mockito.Mockito;

public class TestSpy {

    private static SomeOther manualMockOther = new SomeOther() {

        @Override
        public String doSomething() {
            return null;
        }
    };

    private static SomeOther mockitoOther = Mockito.mock(SomeOther.class);

    private void doTestWithMock(SomeOther other) {
        RealObject realObject = Mockito.spy(new RealObject());
        realObject.setSomeOther(other);
        Mockito.when(realObject.getPackage("a")).thenReturn("1");
        Mockito.when(realObject.getPackage("b")).thenReturn(null);
        Assert.assertEquals("1", realObject.getPublic("a"));
        Assert.assertNull(realObject.getPublic("b"));
    }

    @Test
    public void testWithMockitoSomeOther() {
        doTestWithMock(mockitoOther);
    }

    @Test
    public void testWithManualMockSomeOther() {
        doTestWithMock(manualMockOther);
    }

    public static interface SomeOther {
        String doSomething();
    }

    public static class RealObject {

        private SomeOther other;

        public void setSomeOther(SomeOther other) {
            this.other = other;
        }

        public String getPublic(String var) {
            System.err.println("getPublic");
            return getPackage(var);
        }

        String getPackage(String var) {
            System.err.println("getPackage");
            return other.doSomething();
        }

    }

}

What is the expected output? What do you see instead?

I would expect the 2 test methods to behave the same and pass.
Regardless if SomeOther is a manual or Mockito implementation as I am spying 
the method where it is used.

I see the mockito test fail and the other pass

What version of the product are you using? On what operating system?
1.9.0 , Windows XP.

Please provide any additional information below.

Original issue reported on code.google.com by liamjk...@gmail.com on 11 Apr 2012 at 5:06

GoogleCodeExporter commented 8 years ago
From the first glance it does look "interesting". Can you tell us the stack 
trace (ie where is it failing), also what kind/version of java do you use?

I dont think its related but you can try: make mocked methods public, avoid 
static state.

We'll look at this problem.

Cheers!

Original comment by szcze...@gmail.com on 11 Apr 2012 at 5:34

GoogleCodeExporter commented 8 years ago
Run the code

I cant say more

Original comment by liamjk...@gmail.com on 13 Apr 2012 at 11:56

GoogleCodeExporter commented 8 years ago
>Run the code

can't do it on my phone :) We'll try your sample soon. More details usually 
help because some of us might have an answer straight away.

Original comment by szcze...@gmail.com on 13 Apr 2012 at 6:18

GoogleCodeExporter commented 8 years ago
I think its to do with how your spy() first evaluates the Mock to determine
what to do when its really invoked.
But I didn't trace it very far.

The code example is about as simple as it can be an will run with
JUnit/Mockito required

Original comment by liamjk...@gmail.com on 13 Apr 2012 at 11:24

GoogleCodeExporter commented 8 years ago
Hi,
I traced a little bit this test, and in case of "other" is a mock, I noticed 
that
  Mockito.when(realObject.getPackage("a")).thenReturn("1");
actually mocks other.doSomething() and not realObject.getPackage("a")

So at the 2nd method mocking
  Mockito.when(realObject.getPackage("b")).thenReturn(null);
other.doSomething() will return null, which explains the assert failure.

I will try to investigate further.

Original comment by ludoch...@gmail.com on 20 Jul 2012 at 9:24

GoogleCodeExporter commented 8 years ago
Thanks Ludovic :)

Original comment by brice.du...@gmail.com on 20 Jul 2012 at 10:34