flobacca
flobacca

Reputation: 978

Mockito anyInt not working

I'm new to Mockito and can not get the basic anyInt() method to work. What am I missing? Here's my test.

public class SpanPainterTest {
@Test
  public void simpleTest () {
    SpannableString mockSpanString = mock(SpannableString.class);

    SpanPainter painter = new SpanPainter();
    painter.applyColor(mockSpanString);

    verify(mockSpanString).charAt(anyInt());
    verify(mockSpanString).equals(anyInt());
  }
}

Here's the painter class.

public class SpanPainter {
  public SpannableString applyColor(SpannableString span) {
  span.charAt(7);
  span.equals(0);
  return span;
  }
}

When I comment out 'verify...charAt(anyInt()));' line, then the test passes, so anyInt() works for the equals() method.

Upvotes: 2

Views: 1241

Answers (1)

wjans
wjans

Reputation: 10115

The charAt method is final and final methods cannot be mocked.

Have a look at PowerMock (PowerMockito) to mock final methods.

Upvotes: 3

Related Questions