Expectations order in a regular(non-strict) expectations block matters #393

Closed
shftdlt opened this Issue Feb 11, 2017 · 0 comments

Comments

2 participants
@shftdlt

shftdlt commented Feb 11, 2017

JMockit: 1.30
JUnit: 4.12
JDK: 1.8.0_111

In the sample code, is the difference between test methods matchSpecific and matchSpecific2 as design or a bug?

import mockit.*;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class MockMatchTest {
    @Test
    public void matchAll(@Mocked ContrivedInteger anyInstance) {
        new Expectations() {{
            anyInstance.intValue();
            result = 1;
        }};

        assertEquals(1, new ContrivedInteger().intValue());
        assertEquals(1, new ContrivedInteger(10).intValue());
    }

    @Test
    public void matchSpecific(@Mocked ContrivedInteger anyInstance) {
        new Expectations() {{
            anyInstance.intValue();
            result = 1;

            ContrivedInteger specificInstance = new ContrivedInteger(anyInt);
            specificInstance.intValue();
            result = 2;
        }};

        // specific constructor matching
        assertEquals(2, new ContrivedInteger(10).intValue());

        // why expected: 0
        assertEquals(0, new ContrivedInteger().intValue());
    }

    @Test
    public void matchSpecific2(@Mocked ContrivedInteger anyInstance) {
        new Expectations() {{
            ContrivedInteger specificInstance = new ContrivedInteger(anyInt);
            specificInstance.intValue();
            result = 2;

            anyInstance.intValue();
            result = 1;
        }};

        // specific constructor matching
        assertEquals(2, new ContrivedInteger(100).intValue());

        // why expected: 1
        assertEquals(1, new ContrivedInteger().intValue());
    }

    static class ContrivedInteger {
        Integer value = null;
        public ContrivedInteger() {this(-1);}
        public ContrivedInteger(int value) {this.value = Integer.valueOf(value);}
        public int intValue() {return value.intValue();}
    }
}

@rliesenfeld rliesenfeld self-assigned this Feb 11, 2017

@rliesenfeld rliesenfeld added the bug label Feb 11, 2017

rliesenfeld added a commit that referenced this issue Mar 18, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment