0

JMockit is not returning the Integer set as the return value in an expectation.

public interface Foo {
    Integer getInt();
}

@Test
public void test(@Mocked final Foo foo) {
    final Integer anyInt = 3;
    new Expectations() {{
        foo.getInt(); result = anyInt;
    }};
    assertThat(foo.getInt(), equalTo(anyInt));
}

fails with message:

java.lang.AssertionError:
Expected: <3>
  but: was <0>

Any idea why?

JMockit 1.14

Thanks

beluchin
  • 12,047
  • 4
  • 24
  • 35

1 Answers1

1

The JMockit Expectations API has a set of any fields for argument matching, including anyInt. So, the "anyInt" that appears inside the expectation block is that field, not the local variable of same name.

(If you are using a decent Java IDE, it should show fields with a different color than that used for local variables, making the mistake easy to spot.)

Rogério
  • 16,171
  • 2
  • 50
  • 63