Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

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

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

shftdlt opened this issue Feb 11, 2017 · 0 comments
Assignees
Labels
bug

Comments

@shftdlt
Copy link

@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
2 participants
You can’t perform that action at this time.