Skip to content

Stubbing override, continued #26

@krichardsson

Description

@krichardsson

I'm not happy with the response in issue #5 as the behaviour in Phockito differs from Mockito.
The following test passes in Mockito

package org.richardsson;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class OverwriteTest {
    public static class Foo {
        public int bar() { return -1; }
    }

    @Test
    public void shouldOverride() {
        Foo fooMock = mock(Foo.class);

        when(fooMock.bar()).thenReturn(0);
        when(fooMock.bar()).thenReturn(1);
        when(fooMock.bar()).thenReturn(2);

        assertEquals(2, fooMock.bar());
        assertEquals(2, fooMock.bar());
        assertEquals(2, fooMock.bar());
    }
}

that is, the latest return value is used. To me i makes a lot of sense and is something I often use when writing java code. I usually set up the mocks for the main flow in the the setUp() method which means that test methods that verify that flow do not need any mock code. The test methods that verify other code paths on the other hand, clearly indicate the difference in mock behaviour by overrides. With the current Phockito implementation this is only possible by using reset() which adds noise.

It also turns out that the current Phockito implementation does not behave as described in issue #5, so I suppose it is broken.

when($mock->foo())->return(1);
when($mock->foo())->return(2);
when($mock->foo())->return(3);

// Expected behaviour described in issue #5
$mock->foo(); // returns 1
$mock->foo(); // returns 2
$mock->foo(); // returns 3

// Actual behaviour
$mock->foo(); // returns 1
$mock->foo(); // returns 1
$mock->foo(); // returns 1

// Mockito way
$mock->foo(); // returns 3
$mock->foo(); // returns 3
$mock->foo(); // returns 3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions